Skip to content

Instantly share code, notes, and snippets.

@sfoster
Created January 4, 2011 10:13
Show Gist options
  • Save sfoster/764612 to your computer and use it in GitHub Desktop.
Save sfoster/764612 to your computer and use it in GitHub Desktop.
dojo.data store (e.g. ServiceStore extension) getValue method implementation that allows mapping of attribute names to potentially deep properties
getValue: function(/*Object*/ item, /*String*/property, /*value?*/defaultValue){
// summary:
// Gets the value of an item's 'property'
//
// item:
// The item to get the value from
// property:
// property to look up value for
// defaultValue:
// the default value
var value = void(0), // undefined
attrData = this.itemPropertyMapping[property] || {},
dotpath = attrData.mapping;
if(dotpath) {
// use the mapping to lookup the property value in the item
value = dojo.getObject(dotpath, false, item);
} else {
// fallback to a plain property look up
value = item[property];
}
if(attrData.format) {
value = attrData.format(value, property, attrData);
}
// TODO: lazy-loaded value access not tested
return typeof value !== "undefined" ?
value : // return the plain value since it was found;
(item._loadObject ? // property was not found, maybe because the item is not loaded, we will try to load it synchronously so we can get the property
(dojox.rpc._sync = true) && arguments.callee.call(this,dojox.data.ServiceStore.prototype.loadItem({item:item}) || {}, property, defaultValue) : // load the item and run getValue again
defaultValue);// not in item -> return default value
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment