/* Copyright (c) 2004-2011, The Dojo Foundation All Rights Reserved. Available via Academic Free License >= 2.1 OR the modified BSD license. see: http://dojotoolkit.org/license for details */ if(!dojo._hasResource["dojo.data.ObjectStore"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code. dojo._hasResource["dojo.data.ObjectStore"] = true; dojo.provide("dojo.data.ObjectStore"); dojo.require("dojo.regexp"); dojo.declare("dojo.data.ObjectStore", null,{ objectStore: null, constructor: function(options){ // summary: // A Dojo Data implementation that wraps Dojo object stores for backwards // compatibility. // options: // The configuration information to pass into the data store. // options.objectStore: // The object store to use as the source provider for this data store dojo.mixin(this, options); }, labelProperty: "label", 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 return typeof item.get === "function" ? item.get(property) : property in item ? item[property] : defaultValue; }, getValues: function(item, property){ // summary: // Gets the value of an item's 'property' and returns // it. If this value is an array it is just returned, // if not, the value is added to an array and that is returned. // // item: /* object */ // property: /* string */ // property to look up value for var val = this.getValue(item,property); return val instanceof Array ? val : val === undefined ? [] : [val]; }, getAttributes: function(item){ // summary: // Gets the available attributes of an item's 'property' and returns // it as an array. // // item: /* object */ var res = []; for(var i in item){ if(item.hasOwnProperty(i) && !(i.charAt(0) == '_' && i.charAt(1) == '_')){ res.push(i); } } return res; }, hasAttribute: function(item,attribute){ // summary: // Checks to see if item has attribute // // item: /* object */ // attribute: /* string */ return attribute in item; }, containsValue: function(item, attribute, value){ // summary: // Checks to see if 'item' has 'value' at 'attribute' // // item: /* object */ // attribute: /* string */ // value: /* anything */ return dojo.indexOf(this.getValues(item,attribute),value) > -1; }, isItem: function(item){ // summary: // Checks to see if the argument is an item // // item: /* object */ // attribute: /* string */ // we have no way of determining if it belongs, we just have object returned from // service queries return (typeof item == 'object') && item && !(item instanceof Date); }, isItemLoaded: function(item){ // summary: // Checks to see if the item is loaded. // // item: /* object */ return item && typeof item.load !== "function"; }, loadItem: function(args){ // summary: // Loads an item and calls the callback handler. Note, that this will call the callback // handler even if the item is loaded. Consequently, you can use loadItem to ensure // that an item is loaded is situations when the item may or may not be loaded yet. // If you access a value directly through property access, you can use this to load // a lazy value as well (doesn't need to be an item). // // example: // store.loadItem({ // item: item, // this item may or may not be loaded // onItem: function(item){ // // do something with the item // } // }); var item; if(typeof args.item.load === "function"){ dojo.when(args.item.load(), function(result){ item = result; // in synchronous mode this can allow loadItem to return the value var func = result instanceof Error ? args.onError : args.onItem; if(func){ func.call(args.scope, result); } }); }else if(args.onItem){ // even if it is already loaded, we will use call the callback, this makes it easier to // use when it is not known if the item is loaded (you can always safely call loadItem). args.onItem.call(args.scope, args.item); } return item; }, close: function(request){ return request && request.abort && request.abort(); }, fetch: function(args){ // summary: // See dojo.data.api.Read.fetch // args = args || {}; var self = this; var scope = args.scope || self; var query = args.query; if(typeof query == "object"){ // can be null, but that is ignore by for-in query = dojo.delegate(query); // don't modify the original for(var i in query){ // find any strings and convert them to regular expressions for wildcard support var required = query[i]; if(typeof required == "string"){ query[i] = RegExp("^" + dojo.regexp.escapeString(required, "*?").replace(/\*/g, '.*').replace(/\?/g, '.') + "$", args.queryOptions && args.queryOptions.ignoreCase ? "mi" : "m"); query[i].toString = (function(original){ return function(){ return original; } })(required); } } } var results = this.objectStore.query(query, args); dojo.when(results.total, function(totalCount){ dojo.when(results, function(results){ if(args.onBegin){ args.onBegin.call(scope, totalCount || results.length, args); } if(args.onItem){ for(var i=0; i 0;){ i--; var dirty = dirtyObjects[i]; var object = dirty.object; var old = dirty.old; if(object && old){ // changed for(var j in old){ if(old.hasOwnProperty(j) && object[j] !== old[j]){ this.onSet(object, j, object[j], old[j]); object[j] = old[j]; } } for(j in object){ if(!old.hasOwnProperty(j)){ this.onSet(object, j, object[j]); delete object[j]; } } }else if(!old){ // was an addition, remove it this.onDelete(object); }else{ // was a deletion, we will add it back this.onNew(old); } delete (object || old).__isDirty; dirtyObjects.splice(i, 1); } }, isDirty: function(item){ // summary // returns true if the item is marked as dirty or true if there are any dirty items if(!item){ return !!this._dirtyObjects.length; } return item.__isDirty; }, //Notifcation Support onSet: function(){}, onNew: function(){}, onDelete: function(){} } ); }