Additional object related functions
| ObjectLib | Additional object related functions |
| Functions | |
| $O | wraps an object with the ObjectLib functions. |
| before | Prepends supplied function to the event chain of an object. |
| after | Appends supplied function to the event chain of an object. |
| appendFunction | alias for after |
| applyTo | Copies all properties (including Function properties and “hidden”) of an object to another |
| getKeys | returns an alphabetized list of non-function properties in an 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 supplied object to JSON (http://www.json.org) |
| toStruct | returns a copy of an object with all the function properties removed |
| typeOf | an enhanced replacement of the the Javscript builtin typeof function. |
| setByPath | sets a property or nested object property of this object |
| setDefaultProperties | sets default properties on an object |
| forEach | loops over each non-function property of an 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 obj. |
| filter | returns new Object with only the key/values from obj object that pass a test function |
| toArray | returns an Array of objects with a “key” property and a “value” property mapping to the keys and values of this object |
if ( typeof $O = = "undefined" )
wraps an object with the ObjectLib functions.
| obj | object to wrap |
returns obj with all functions in ObjectLib attached, with object as the target
var thing={
name:"thing",
purpose"do Stuff!"
}
//via ObjectLib
ObjectLib.setDefaultProperties(thing,{
newProp:"I'm a new property"
})
//via $O
$O(thing).setDefaultProperties({
newProp2:"another new prop"
})
ObjectLib.before=function( obj, functionName, functionObj )
Prepends supplied function to the event chain of an 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.
var obj={
stuff:function (text){
Myna.println("in orig")
return text + " " + this.myVal;
},
myVal:"firstObj"
}
var obj2={
myVal:"secondObj"
}
ObjectLib.before(obj,"stuff",function(text){
var chain = arguments.callee.chain;
Myna.println("in before")
chain.args[0] = "before " + text
if (text == "dude!"){
// exit now with this return value, nothing after will be executed
chain.exit("sweet!")
}
})
ObjectLib.after(obj,"stuff",function(text){
var chain = arguments.callee.chain;
Myna.println("in after")
return chain.lastReturn + " after "
})
Myna.println(obj.stuff("woot!") +"<hr>");
Myna.println(obj.stuff("dude!") +"<hr>");
obj2.stuff = obj.stuff;
Myna.println(obj2.stuff("woot!") +"<hr>");
ObjectLib.after=function( obj, functionName, functionObj )
Appends supplied function to the event chain of an 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.
(end)
alias for after
ObjectLib.applyTo=function( obj, target, shouldOverwrite )
Copies all properties (including Function properties and “hidden”) of an object to another
| obj | object to copy from |
| 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 copying the properties of an object to a local scope by applying to ‘this’, or 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
ObjectLib.applyTo(Myna,this);
ObjectLib.getKeys = function( obj )
returns an alphabetized list of non-function properties in an object
| obj | object to examine |
An alphabetized array of properties in an object
ObjectLib.getProperties = function( obj )
returns an alphabetized list of all properties in an object
| obj | object to examine |
An alphabetized array of properties in an object
ObjectLib.hideProperty = function( obj, property )
Server-Only set the “DONTENUM” attribute of the supplied property
| obj | object to examine |
| 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”
ObjectLib.checkRequired=function ( obj, required )
Ensures that certain properties defined.
| obj | object to examine |
| 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 an object. The first time a property is not found, an exception is raised.
ObjectLib.toJson=function( obj )
Converts the supplied object to JSON (http://www.json.org)
| obj | object to convert |
JSON string that represents obj
Adapted from http://www.json.org/json.js
Attempts to convert obj 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());
ObjectLib.toStruct=function( obj )
returns a copy of an object with all the function properties removed
| object | object to inspect |
ObjectLib.typeOf=function( object )
an enhanced replacement of the the Javscript builtin typeof function.
| object | object to inspect |
a string representing the type of the object
The builtin JavaScript typeof function does not identify some stamndard objects, specifically Arrays, Dates, and Nulls. When running in ObjectLib, it is also important to know when the object is a Java object. This function returns the standard typeof strings as well as the following:
ObjectLib.setByPath=function ( obj, path, value )
sets a property or nested object property of this object
| obj | object to apply to |
| path | dot separated path to the property to set |
| value | value to set |
obj
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 = ObjectLib.setByPath({},"Users.336642.firstName","Mark")
ObjectLib.setByPath(result,"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 = {}
ObjectLib.setByPath(result,"Users[*].firstName","Mark")
ObjectLib.setByPath(result,"Users[0].firstName","Bob")
// result Equals
// {
// Users:[
// {
// firstName:"Bob"
// },
// {
// firstName:"Mark"
// }
// ]
// }This function is applied automatically against $req.data for params that contain periods
ObjectLib.setDefaultProperties=function ( obj, defaults, looseMatch )
sets default properties on an object
| obj | object to apply to |
| 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. |
obj
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
});
ObjectLib.forEach=function ( obj, func )
loops over each non-function property of an object an executes the supplied function against it.
| obj | Object to loop over |
| 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 an object |
This function is modeled after the JS function Array.forEach.
var emp ={
id:12,
name:"Bob"
occupation:"being awsome",
isDeceased:false
}
ObjectLib.forEach(emp,function(element,name,object){
alert(name + ": " + element +"<br>");
})
ObjectLib.map=function ( obj, func )
returns new Object with the results of calling a provided function on every non-function element in obj.
| obj | Object to loop over |
| 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
}
var fixedEmp = ObjectLib.map(emp,function(element,name,object){
if (element === null) {
return ""
} else {
return element
}
})
ObjectLib.filter=function ( obj, func )
returns new Object with only the key/values from obj 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
}
var fixedEmp = ObjectLib.filter(emp,function(element,name,object){
return element !== null
})
ObjectLib.toArray=function ( obj, includeFunctions, localOnly )
returns an Array of objects with a “key” property and a “value” property mapping to the keys and values of this object
| obj | Object to loop over |
| includeFunctions | Optional, default false By default only properties that are not functions are mapped. Set this to true to include functions |
| localOnly | Optional, default false By default both local and prototype properties are mapped, set this to true limit to only local properties |
if Myna.DataSet is available, then a DataSet is returned, which allows recreating the object via result.toMap(“key”,”value”)
var obj = {first_name:"Bob",last_name:"Dobb"}
var array = ObjectLib.toArray(obj)
//returns [{key:"first_name",value:"Bob"},{key:"lasst_name",value:"Dobb"}]wraps an object with the ObjectLib functions.
if ( typeof $O = = "undefined" )
Prepends supplied function to the event chain of an object.
ObjectLib.before=function( obj, functionName, functionObj )
Appends supplied function to the event chain of an object.
ObjectLib.after=function( obj, functionName, functionObj )
Copies all properties (including Function properties and “hidden”) of an object to another
ObjectLib.applyTo=function( obj, target, shouldOverwrite )
returns an alphabetized list of non-function properties in an object
ObjectLib.getKeys = function( obj )
returns an alphabetized list of all properties in an object
ObjectLib.getProperties = function( obj )
Server-Only set the “DONTENUM” attribute of the supplied property
ObjectLib.hideProperty = function( obj, property )
Ensures that certain properties defined.
ObjectLib.checkRequired=function ( obj, required )
Converts the supplied object to JSON (http://www.json.org)
ObjectLib.toJson=function( obj )
returns a copy of an object with all the function properties removed
ObjectLib.toStruct=function( obj )
an enhanced replacement of the the Javscript builtin typeof function.
ObjectLib.typeOf=function( object )
sets a property or nested object property of this object
ObjectLib.setByPath=function ( obj, path, value )
sets default properties on an object
ObjectLib.setDefaultProperties=function ( obj, defaults, looseMatch )
loops over each non-function property of an object an executes the supplied function against it.
ObjectLib.forEach=function ( obj, func )
returns new Object with the results of calling a provided function on every non-function element in obj.
ObjectLib.map=function ( obj, func )
returns new Object with only the key/values from obj object that pass a test function
ObjectLib.filter=function ( obj, func )
returns an Array of objects with a “key” property and a “value” property mapping to the keys and values of this object
ObjectLib.toArray=function ( obj, includeFunctions, localOnly )
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 )