Additional functions on the JS Object object
Each function here comes from <ObjectLib.js>, so including this file is only necessary to apply these functions to the Object prototype
| Object | Additional functions on the JS Object object |
| Functions | |
| before | Prepends supplied function to the event chain of this object. |
| after | Appends supplied function to the event chain of this object. |
| old_before | /* Function: old_after DEPRECATED This function is for backwards compatibility only. |
| old_after | DEPRECATED This function is for backwards compatibility only. |
| appendFunction | alias for after |
| applyTo | Copies all properties (including Function properties) of this object to another |
| getKeys | returns an alphabetized list of non-function properties in this object |
| getProperties | returns an alphabetized list of all properties in an object |
| hideProperty | Server-Only set the “DONTENUM” attribute of the supplied property |
| checkRequired | Ensures that certain properties defined. |
| toJson | Converts the this object to JSON (http://www.json.org) |
| toStruct | returns a copy of an object with all the function properties removed |
| setByPath | sets a property or nested object property of this object |
| setDefaultProperties | sets default properties on this object |
| forEach | loops over each non-function property of this object an executes the supplied function against it. |
| map | returns new Object with the results of calling a provided function on every non-function element in this. |
| filter | returns new Object with only the key/values from this object that pass a test function |
| copy | returns a new object with the same properties as the supplied object |
| createProxy | returns a proxy object where the functions and properties actually refer to this object |
Object.prototype.before=function( functionName, functionObj )
Prepends supplied function to the event chain of this object.
| obj | object to apply to |
| functionName | name of the function on an object to modify |
| functionObj | function object to append |
Existing functions are preserved and executed after the supplied function. This is a shortcut for creating chain functions and is the equivalent of
obj[functionName] = obj[functionName].before(functionObj)
See Function.createChainFunction for how chain functions work.
// example of creating an audit function
dm = new Myna.DataManager("hr")
dm.managerTemplate.before("saveField",function(fieldName,newVal){
var chain = arguments.callee.chain;
//save audit table manager
if (!this.audiManager) {
this.auditManager = new Myna.DataManager("audit_db").getManager("audit_Table")
}
this.audiManager.create({
ts:new Date(),
user_id:$cookie.getAuthUserId,
table:this.table.tableName,
column:fieldName,
old_val:this.data[fieldName],
new_val:newVal
})
})
Object.prototype.after=function( functionName, functionObj )
Appends supplied function to the event chain of this object.
| obj | object to apply to |
| functionName | name of the function on an object to modify |
| functionObj | function object to append |
Existing functions are preserved and executed after the supplied function. This is a shortcut for creating chain functions and is the equivalent of
obj[functionName] = obj[functionName].before(functionObj)
See Function.createChainFunction for how chain functions work.
// example of adding extra functions to bean objects
dm = new Myna.DataManager("hr")
dm.managerTemplate.after("getById",function(id){
var chain = arguments.callee.chain;
if (this.table.tableName == "employee"){
chain.lastReturn.getDirectReports = function(){
return this.manager.findBeans({
manager_id:this.employee_id
})
}
}
return chain.lastReturn;
})
Object.prototype.old_before=function( functionName, functionObj )
/* Function: old_after DEPRECATED This function is for backwards compatibility only. See before
Prepends supplied function to the event chain of this object.
| functionName | name of the function on an object to modify |
| functionObj | function object to append |
Existing functions are preserved and executed after the supplied function.
functionObj will have properties added to it that can be accessed via arguments.callee.chain from inside the function
| returnValue | default undefined If this is defined after functionObj completes, then this value will be returned instead of executing the original function. |
| originalFunction | This is a reference to the original function, bound to this object. This is useful in conjunction with returnValue to call the original function with altered parameters and return the result |
// example of setting calculated values during object creation
dm = new Myna.DataManager("hr")
dm.managerTemplate.before("create",function(obj){
var chain = arguments.callee.chain;
if (this.table.tableName == "employee"){
if (!obj.salary){
obj.salary = calcBaseSalary(obj)
}
chain.returnValue=chain.originalFunction(obj);
}
})
Object.prototype.old_after=function( functionName, functionObj )
DEPRECATED This function is for backwards compatibility only. See after
Appends supplied function to the event chain of this object.
| functionName | name of the function on an object to modify |
| functionObj | function object to append |
Existing functions are preserved and executed in the order they were declared. If the function functionName does not exist, it will be created.
The resulting chain will return the result of the original function, unless overridden in arguments.callee.chain.returnValue
functionObj will have properties added to it that can be accessed via arguments.callee.chain from inside the function.
| returnValue | default return value from original function This is the value that will be returned. This can be overwritten by functionObj |
| originalFunction | This is a reference to the original function, bound to this object. |
// example of adding extra functions to bean objects
dm = new Myna.DataManager("hr")
dm.managerTemplate.after("getById",function(id){
var chain = arguments.callee.chain;
if (this.table.tableName == "employee"){
chain.returnValue.getDirectReports = function(){
return this.manager.findBeans({
manager_id:this.employee_id
})
}
}
})
alias for after
Object.prototype.applyTo=function( target, shouldOverwrite )
Copies all properties (including Function properties) of this object to another
| target | object to copy to |
| shouldOverwrite | Optional, default false Should existing properties in target be replaced by the properties in source? |
target
This can be used for simulating inheritance (even multiple inheritance) on instantiated objects by copying the properties of another object
// Make Myna's functions such as abort() and dump() available
// without the Myna prefix
Myna.applyTo(this);
Object.prototype.getKeys = function( obj )
returns an alphabetized list of non-function properties in this object
| obj | Optional, default this object to examine |
An alphabetized array of properties in an object
Object.prototype.getProperties = function( obj )
returns an alphabetized list of all properties in an object
| obj | Optional, default this object to examine |
An alphabetized array of properties in an object
Object.prototype.hideProperty = function( property )
Server-Only set the “DONTENUM” attribute of the supplied property
| property | propery to modify |
Set the the “DONTENUM” attribute on the defined property. This means that this property will not appear in getProperties or <Myna.Dump> or forEach or any other function that loops over enumerable properties. Use this to “hide” functions and properties that you do not want to be considered “data”
Object.prototype.checkRequired=function ( required )
Ensures that certain properties defined.
| required | Array of property name strings to look for |
void
This function is intended for Javascript Objects being used as data containers. Particularly JS objects passed as function parameters.
This function simply checks to see if every string in the required array has a corresponding property in this Object. The first time a property is not found, an exception is raised.
Object.prototype.toJson=function( indent )
Converts the this object to JSON (http://www.json.org)
JSON string that represents this object
Adapted from http://www.json.org/json.js
Attempts to convert this object to JSON. This is best used on simple Objects and Arrays.
//this code might be called by an AJAX callback
var qry=new Query({
dataSource:":mem:",
sql:"select * from event"
});
$res.print(qry.data.toJson());
Object.prototype.toStruct=function()
returns a copy of an object with all the function properties removed
| object | object to inspect |
Object.prototype.setByPath=function ( path, value )
sets a property or nested object property of this object
| path | dot or array separated path to the property to set |
| value | value to set |
this
Often times it is convenient to store key value pairs as a dot separated path and a value, especially in HTML forms which do not support structured parameters like so:
<input name="Users.336642.firstName" value = "Mark">
Calling this function against an object will walk the nested object tree, creating objects as necessary, until the final property is set to the value
var result = {}
result.setByPath("Users.336642.firstName","Mark")
result.setByPath("Users.536642.firstName","Bob")
// result Equals
// {
// Users:{
// "336642":{
// firstName:"Mark"
// },
// "536642":{
// firstName:"Bob"
// },
// }
// }
// the * means append otherwise the array index is used even if out of order
var result = {}
result.setByPath("Users[*].firstName","Mark")
result.setByPath("Users[0].firstName","Bob")
// result Equals
// {
// Users:[
// {
// firstName:"Bob"
// },
// {
// firstName:"Mark"
// }
// ]
// }This function is applied automatically against $req.data for params that contain periods
Object.prototype.setDefaultProperties=function ( defaults, looseMatch )
sets default properties on this object
| defaults | Object that represents the default properties |
| looseMatch | If true, consider “null” values and 0 length strings to be the same as undefined. By default, only strictly undefined properties are overwritten by their defaults. |
this
Every property in defaults is checked against this. If the property is undefined in this, it is copied from defaults.
$res.data.setDefaultProperties({
name:"bob",
isDeceased:false
});
Object.prototype.forEach=function ( func )
loops over each non-function property of this object an executes the supplied function against it.
| callback | Function to execute. See below for the parameters it will be passed |
| element | the value of property |
| name | the name of the property |
| object | a reference to this object |
This function is modeled after the JS function Array.forEach.
{
id:12,
name:"Bob"
occupation:"being awsome",
isDeceased:false
}.forEach(function(element,name,object){
ObjectLib.print(name + ": " + element +"<br>");
})
Object.prototype.map=function map( func )
returns new Object with the results of calling a provided function on every non-function element in this.
| func | Function to execute. See below for the parameters it will be passed |
| element | the value of property |
| name | the name of the property |
| object | a reference to this object |
This function is modeled after the JS function Array.map.
//make sure null values come across as empty strings
var emp = {
id:12,
name:"Bob",
age:null,
occupation:"being awesome",
isDeceased:false
}.map(function(element,name,object){
if (element === null) {
return ""
} else {
return element
}
})
Object.prototype.filter=function ( func )
returns new Object with only the key/values from this object that pass a test function
| obj | Object to loop over |
| func | Function to execute. return true to include this key/value See below for the parameters it will be passed |
| element | the value of property |
| name | the name of the property |
| object | a reference to this object |
This function is modeled after the JS function Array.filter.
// remove null values
var emp = {
id:12,
name:"Bob",
age:null,
occupation:"being awesome",
isDeceased:false
}.filter(function(element,name,object){
return element !== null
})
Object.prototype.copy=function ( deep )
returns a new object with the same properties as the supplied object
| obj | object to apply to |
| deep | optional, default false If true, a deep copy is made where no property or child of a property in the new object is shared with the original object. |
Only works with pure JavaScript, and functions will be stripped
by default, this performs a shallow copy by creating a new object and copying the properties of the old object onto it.
if deep is true, then the object is decompiled to source and the compiled as a new object. Deep copies will fail if the object contains java objects, and all functions are removed, regardless. Deep copies are useful for “sanitizing” a complex data object for storage, transmission, or return from a function.
var dontChangeName =function(obj){
//make a shallow copy of object so we can alter it's properties
var myObj = obj.copy();
myObj.name = "stan";
}
var bob ={
name:"bob"
}
dontChangeName(bob);
//bob is still "bob"
var dataChunk={
_data:[1,2,3,4],
getData:function(){
//deep copy, user can't change contents of this._data
return this._data.copy(true);
}
}
var data = dataChunk.getData();
data.push(5); //doesn't affect dataChunk._data;
Object.prototype.createProxy=function Object_createProxy( target, overwrite )
returns a proxy object where the functions and properties actually refer to this object
| target | Optional, default {} If defined, properties and functions will be overlaid on the target object instead of creating a new object. |
| overwrite | Optional,default false Only applies if target is defined. if true, then existing properties on _target will be overwritten by this proxy versions. |
This purpose of this function is to make composting easier. A great example is if you want to extend a DAO without using inheritance, like so:
Prepends supplied function to the event chain of this object.
Object.prototype.before=function( functionName, functionObj )
Appends supplied function to the event chain of this object.
Object.prototype.after=function( functionName, functionObj )
/* Function: old_after DEPRECATED This function is for backwards compatibility only.
Object.prototype.old_before=function( functionName, functionObj )
DEPRECATED This function is for backwards compatibility only.
Object.prototype.old_after=function( functionName, functionObj )
Copies all properties (including Function properties) of this object to another
Object.prototype.applyTo=function( target, shouldOverwrite )
returns an alphabetized list of non-function properties in this object
Object.prototype.getKeys = function( obj )
returns an alphabetized list of all properties in an object
Object.prototype.getProperties = function( obj )
Server-Only set the “DONTENUM” attribute of the supplied property
Object.prototype.hideProperty = function( property )
Ensures that certain properties defined.
Object.prototype.checkRequired=function ( required )
Converts the this object to JSON (http://www.json.org)
Object.prototype.toJson=function( indent )
returns a copy of an object with all the function properties removed
Object.prototype.toStruct=function()
sets a property or nested object property of this object
Object.prototype.setByPath=function ( path, value )
sets default properties on this object
Object.prototype.setDefaultProperties=function ( defaults, looseMatch )
loops over each non-function property of this object an executes the supplied function against it.
Object.prototype.forEach=function ( func )
returns new Object with the results of calling a provided function on every non-function element in this.
Object.prototype.map=function map( func )
returns new Object with only the key/values from this object that pass a test function
Object.prototype.filter=function ( func )
returns a new object with the same properties as the supplied object
Object.prototype.copy=function ( deep )
returns a proxy object where the functions and properties actually refer to this object
Object.prototype.createProxy=function Object_createProxy( target, overwrite )
returns a function that will execute a chain of functions when called.
Function.createChainFunction=function( initialChain )
Executes a provided function once per array element.
if ( !Array.prototype.forEach )
Creates a new array with the results of calling a provided function on every element in this array.
if ( !Array.prototype.map )
Creates a new array with all elements that pass the test implemented by the provided function.
if ( !Array.prototype.filter )