]> git.wh0rd.org - tt-rss.git/blobdiff - lib/dojo/data/ItemFileWriteStore.js
build custom layer of Dojo to speed up loading of tt-rss (refs #293)
[tt-rss.git] / lib / dojo / data / ItemFileWriteStore.js
index c891c14e77c5610e665e4a8a065410f2ef771a8a..8782dce05afc099773ce919b8cf77df11db0a0e7 100644 (file)
 */
 
 
-if(!dojo._hasResource["dojo.data.ItemFileWriteStore"]){
-dojo._hasResource["dojo.data.ItemFileWriteStore"]=true;
+if(!dojo._hasResource["dojo.data.ItemFileWriteStore"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
+dojo._hasResource["dojo.data.ItemFileWriteStore"] = true;
 dojo.provide("dojo.data.ItemFileWriteStore");
 dojo.require("dojo.data.ItemFileReadStore");
-dojo.declare("dojo.data.ItemFileWriteStore",dojo.data.ItemFileReadStore,{constructor:function(_1){
-this._features["dojo.data.api.Write"]=true;
-this._features["dojo.data.api.Notification"]=true;
-this._pending={_newItems:{},_modifiedItems:{},_deletedItems:{}};
-if(!this._datatypeMap["Date"].serialize){
-this._datatypeMap["Date"].serialize=function(_2){
-return dojo.date.stamp.toISOString(_2,{zulu:true});
-};
-}
-if(_1&&(_1.referenceIntegrity===false)){
-this.referenceIntegrity=false;
-}
-this._saveInProgress=false;
-},referenceIntegrity:true,_assert:function(_3){
-if(!_3){
-throw new Error("assertion failed in ItemFileWriteStore");
-}
-},_getIdentifierAttribute:function(){
-var _4=this.getFeatures()["dojo.data.api.Identity"];
-return _4;
-},newItem:function(_5,_6){
-this._assert(!this._saveInProgress);
-if(!this._loadFinished){
-this._forceLoad();
-}
-if(typeof _5!="object"&&typeof _5!="undefined"){
-throw new Error("newItem() was passed something other than an object");
-}
-var _7=null;
-var _8=this._getIdentifierAttribute();
-if(_8===Number){
-_7=this._arrayOfAllItems.length;
-}else{
-_7=_5[_8];
-if(typeof _7==="undefined"){
-throw new Error("newItem() was not passed an identity for the new item");
-}
-if(dojo.isArray(_7)){
-throw new Error("newItem() was not passed an single-valued identity");
-}
-}
-if(this._itemsByIdentity){
-this._assert(typeof this._itemsByIdentity[_7]==="undefined");
-}
-this._assert(typeof this._pending._newItems[_7]==="undefined");
-this._assert(typeof this._pending._deletedItems[_7]==="undefined");
-var _9={};
-_9[this._storeRefPropName]=this;
-_9[this._itemNumPropName]=this._arrayOfAllItems.length;
-if(this._itemsByIdentity){
-this._itemsByIdentity[_7]=_9;
-_9[_8]=[_7];
-}
-this._arrayOfAllItems.push(_9);
-var _a=null;
-if(_6&&_6.parent&&_6.attribute){
-_a={item:_6.parent,attribute:_6.attribute,oldValue:undefined};
-var _b=this.getValues(_6.parent,_6.attribute);
-if(_b&&_b.length>0){
-var _c=_b.slice(0,_b.length);
-if(_b.length===1){
-_a.oldValue=_b[0];
-}else{
-_a.oldValue=_b.slice(0,_b.length);
-}
-_c.push(_9);
-this._setValueOrValues(_6.parent,_6.attribute,_c,false);
-_a.newValue=this.getValues(_6.parent,_6.attribute);
-}else{
-this._setValueOrValues(_6.parent,_6.attribute,_9,false);
-_a.newValue=_9;
-}
-}else{
-_9[this._rootItemPropName]=true;
-this._arrayOfTopLevelItems.push(_9);
-}
-this._pending._newItems[_7]=_9;
-for(var _d in _5){
-if(_d===this._storeRefPropName||_d===this._itemNumPropName){
-throw new Error("encountered bug in ItemFileWriteStore.newItem");
-}
-var _e=_5[_d];
-if(!dojo.isArray(_e)){
-_e=[_e];
-}
-_9[_d]=_e;
-if(this.referenceIntegrity){
-for(var i=0;i<_e.length;i++){
-var _f=_e[i];
-if(this.isItem(_f)){
-this._addReferenceToMap(_f,_9,_d);
-}
-}
-}
-}
-this.onNew(_9,_a);
-return _9;
-},_removeArrayElement:function(_10,_11){
-var _12=dojo.indexOf(_10,_11);
-if(_12!=-1){
-_10.splice(_12,1);
-return true;
-}
-return false;
-},deleteItem:function(_13){
-this._assert(!this._saveInProgress);
-this._assertIsItem(_13);
-var _14=_13[this._itemNumPropName];
-var _15=this.getIdentity(_13);
-if(this.referenceIntegrity){
-var _16=this.getAttributes(_13);
-if(_13[this._reverseRefMap]){
-_13["backup_"+this._reverseRefMap]=dojo.clone(_13[this._reverseRefMap]);
-}
-dojo.forEach(_16,function(_17){
-dojo.forEach(this.getValues(_13,_17),function(_18){
-if(this.isItem(_18)){
-if(!_13["backupRefs_"+this._reverseRefMap]){
-_13["backupRefs_"+this._reverseRefMap]=[];
-}
-_13["backupRefs_"+this._reverseRefMap].push({id:this.getIdentity(_18),attr:_17});
-this._removeReferenceFromMap(_18,_13,_17);
-}
-},this);
-},this);
-var _19=_13[this._reverseRefMap];
-if(_19){
-for(var _1a in _19){
-var _1b=null;
-if(this._itemsByIdentity){
-_1b=this._itemsByIdentity[_1a];
-}else{
-_1b=this._arrayOfAllItems[_1a];
-}
-if(_1b){
-for(var _1c in _19[_1a]){
-var _1d=this.getValues(_1b,_1c)||[];
-var _1e=dojo.filter(_1d,function(_1f){
-return !(this.isItem(_1f)&&this.getIdentity(_1f)==_15);
-},this);
-this._removeReferenceFromMap(_13,_1b,_1c);
-if(_1e.length<_1d.length){
-this._setValueOrValues(_1b,_1c,_1e,true);
-}
-}
-}
-}
-}
-}
-this._arrayOfAllItems[_14]=null;
-_13[this._storeRefPropName]=null;
-if(this._itemsByIdentity){
-delete this._itemsByIdentity[_15];
-}
-this._pending._deletedItems[_15]=_13;
-if(_13[this._rootItemPropName]){
-this._removeArrayElement(this._arrayOfTopLevelItems,_13);
-}
-this.onDelete(_13);
-return true;
-},setValue:function(_20,_21,_22){
-return this._setValueOrValues(_20,_21,_22,true);
-},setValues:function(_23,_24,_25){
-return this._setValueOrValues(_23,_24,_25,true);
-},unsetAttribute:function(_26,_27){
-return this._setValueOrValues(_26,_27,[],true);
-},_setValueOrValues:function(_28,_29,_2a,_2b){
-this._assert(!this._saveInProgress);
-this._assertIsItem(_28);
-this._assert(dojo.isString(_29));
-this._assert(typeof _2a!=="undefined");
-var _2c=this._getIdentifierAttribute();
-if(_29==_2c){
-throw new Error("ItemFileWriteStore does not have support for changing the value of an item's identifier.");
-}
-var _2d=this._getValueOrValues(_28,_29);
-var _2e=this.getIdentity(_28);
-if(!this._pending._modifiedItems[_2e]){
-var _2f={};
-for(var key in _28){
-if((key===this._storeRefPropName)||(key===this._itemNumPropName)||(key===this._rootItemPropName)){
-_2f[key]=_28[key];
-}else{
-if(key===this._reverseRefMap){
-_2f[key]=dojo.clone(_28[key]);
-}else{
-_2f[key]=_28[key].slice(0,_28[key].length);
-}
-}
-}
-this._pending._modifiedItems[_2e]=_2f;
-}
-var _30=false;
-if(dojo.isArray(_2a)&&_2a.length===0){
-_30=delete _28[_29];
-_2a=undefined;
-if(this.referenceIntegrity&&_2d){
-var _31=_2d;
-if(!dojo.isArray(_31)){
-_31=[_31];
-}
-for(var i=0;i<_31.length;i++){
-var _32=_31[i];
-if(this.isItem(_32)){
-this._removeReferenceFromMap(_32,_28,_29);
-}
-}
-}
-}else{
-var _33;
-if(dojo.isArray(_2a)){
-var _34=_2a;
-_33=_2a.slice(0,_2a.length);
-}else{
-_33=[_2a];
-}
-if(this.referenceIntegrity){
-if(_2d){
-var _31=_2d;
-if(!dojo.isArray(_31)){
-_31=[_31];
-}
-var map={};
-dojo.forEach(_31,function(_35){
-if(this.isItem(_35)){
-var id=this.getIdentity(_35);
-map[id.toString()]=true;
-}
-},this);
-dojo.forEach(_33,function(_36){
-if(this.isItem(_36)){
-var id=this.getIdentity(_36);
-if(map[id.toString()]){
-delete map[id.toString()];
-}else{
-this._addReferenceToMap(_36,_28,_29);
-}
-}
-},this);
-for(var rId in map){
-var _37;
-if(this._itemsByIdentity){
-_37=this._itemsByIdentity[rId];
-}else{
-_37=this._arrayOfAllItems[rId];
-}
-this._removeReferenceFromMap(_37,_28,_29);
-}
-}else{
-for(var i=0;i<_33.length;i++){
-var _32=_33[i];
-if(this.isItem(_32)){
-this._addReferenceToMap(_32,_28,_29);
-}
-}
-}
-}
-_28[_29]=_33;
-_30=true;
-}
-if(_2b){
-this.onSet(_28,_29,_2d,_2a);
-}
-return _30;
-},_addReferenceToMap:function(_38,_39,_3a){
-var _3b=this.getIdentity(_39);
-var _3c=_38[this._reverseRefMap];
-if(!_3c){
-_3c=_38[this._reverseRefMap]={};
-}
-var _3d=_3c[_3b];
-if(!_3d){
-_3d=_3c[_3b]={};
-}
-_3d[_3a]=true;
-},_removeReferenceFromMap:function(_3e,_3f,_40){
-var _41=this.getIdentity(_3f);
-var _42=_3e[this._reverseRefMap];
-var _43;
-if(_42){
-for(_43 in _42){
-if(_43==_41){
-delete _42[_43][_40];
-if(this._isEmpty(_42[_43])){
-delete _42[_43];
-}
-}
-}
-if(this._isEmpty(_42)){
-delete _3e[this._reverseRefMap];
-}
-}
-},_dumpReferenceMap:function(){
-var i;
-for(i=0;i<this._arrayOfAllItems.length;i++){
-var _44=this._arrayOfAllItems[i];
-if(_44&&_44[this._reverseRefMap]){
-}
-}
-},_getValueOrValues:function(_45,_46){
-var _47=undefined;
-if(this.hasAttribute(_45,_46)){
-var _48=this.getValues(_45,_46);
-if(_48.length==1){
-_47=_48[0];
-}else{
-_47=_48;
-}
-}
-return _47;
-},_flatten:function(_49){
-if(this.isItem(_49)){
-var _4a=_49;
-var _4b=this.getIdentity(_4a);
-var _4c={_reference:_4b};
-return _4c;
-}else{
-if(typeof _49==="object"){
-for(var _4d in this._datatypeMap){
-var _4e=this._datatypeMap[_4d];
-if(dojo.isObject(_4e)&&!dojo.isFunction(_4e)){
-if(_49 instanceof _4e.type){
-if(!_4e.serialize){
-throw new Error("ItemFileWriteStore:  No serializer defined for type mapping: ["+_4d+"]");
-}
-return {_type:_4d,_value:_4e.serialize(_49)};
-}
-}else{
-if(_49 instanceof _4e){
-return {_type:_4d,_value:_49.toString()};
-}
-}
-}
-}
-return _49;
-}
-},_getNewFileContentString:function(){
-var _4f={};
-var _50=this._getIdentifierAttribute();
-if(_50!==Number){
-_4f.identifier=_50;
-}
-if(this._labelAttr){
-_4f.label=this._labelAttr;
-}
-_4f.items=[];
-for(var i=0;i<this._arrayOfAllItems.length;++i){
-var _51=this._arrayOfAllItems[i];
-if(_51!==null){
-var _52={};
-for(var key in _51){
-if(key!==this._storeRefPropName&&key!==this._itemNumPropName&&key!==this._reverseRefMap&&key!==this._rootItemPropName){
-var _53=key;
-var _54=this.getValues(_51,_53);
-if(_54.length==1){
-_52[_53]=this._flatten(_54[0]);
-}else{
-var _55=[];
-for(var j=0;j<_54.length;++j){
-_55.push(this._flatten(_54[j]));
-_52[_53]=_55;
-}
-}
-}
-}
-_4f.items.push(_52);
-}
-}
-var _56=true;
-return dojo.toJson(_4f,_56);
-},_isEmpty:function(_57){
-var _58=true;
-if(dojo.isObject(_57)){
-var i;
-for(i in _57){
-_58=false;
-break;
-}
-}else{
-if(dojo.isArray(_57)){
-if(_57.length>0){
-_58=false;
-}
-}
-}
-return _58;
-},save:function(_59){
-this._assert(!this._saveInProgress);
-this._saveInProgress=true;
-var _5a=this;
-var _5b=function(){
-_5a._pending={_newItems:{},_modifiedItems:{},_deletedItems:{}};
-_5a._saveInProgress=false;
-if(_59&&_59.onComplete){
-var _5c=_59.scope||dojo.global;
-_59.onComplete.call(_5c);
-}
-};
-var _5d=function(err){
-_5a._saveInProgress=false;
-if(_59&&_59.onError){
-var _5e=_59.scope||dojo.global;
-_59.onError.call(_5e,err);
-}
-};
-if(this._saveEverything){
-var _5f=this._getNewFileContentString();
-this._saveEverything(_5b,_5d,_5f);
-}
-if(this._saveCustom){
-this._saveCustom(_5b,_5d);
-}
-if(!this._saveEverything&&!this._saveCustom){
-_5b();
-}
-},revert:function(){
-this._assert(!this._saveInProgress);
-var _60;
-for(_60 in this._pending._modifiedItems){
-var _61=this._pending._modifiedItems[_60];
-var _62=null;
-if(this._itemsByIdentity){
-_62=this._itemsByIdentity[_60];
-}else{
-_62=this._arrayOfAllItems[_60];
-}
-_61[this._storeRefPropName]=this;
-for(key in _62){
-delete _62[key];
-}
-dojo.mixin(_62,_61);
-}
-var _63;
-for(_60 in this._pending._deletedItems){
-_63=this._pending._deletedItems[_60];
-_63[this._storeRefPropName]=this;
-var _64=_63[this._itemNumPropName];
-if(_63["backup_"+this._reverseRefMap]){
-_63[this._reverseRefMap]=_63["backup_"+this._reverseRefMap];
-delete _63["backup_"+this._reverseRefMap];
-}
-this._arrayOfAllItems[_64]=_63;
-if(this._itemsByIdentity){
-this._itemsByIdentity[_60]=_63;
-}
-if(_63[this._rootItemPropName]){
-this._arrayOfTopLevelItems.push(_63);
-}
-}
-for(_60 in this._pending._deletedItems){
-_63=this._pending._deletedItems[_60];
-if(_63["backupRefs_"+this._reverseRefMap]){
-dojo.forEach(_63["backupRefs_"+this._reverseRefMap],function(_65){
-var _66;
-if(this._itemsByIdentity){
-_66=this._itemsByIdentity[_65.id];
-}else{
-_66=this._arrayOfAllItems[_65.id];
-}
-this._addReferenceToMap(_66,_63,_65.attr);
-},this);
-delete _63["backupRefs_"+this._reverseRefMap];
-}
-}
-for(_60 in this._pending._newItems){
-var _67=this._pending._newItems[_60];
-_67[this._storeRefPropName]=null;
-this._arrayOfAllItems[_67[this._itemNumPropName]]=null;
-if(_67[this._rootItemPropName]){
-this._removeArrayElement(this._arrayOfTopLevelItems,_67);
-}
-if(this._itemsByIdentity){
-delete this._itemsByIdentity[_60];
-}
-}
-this._pending={_newItems:{},_modifiedItems:{},_deletedItems:{}};
-return true;
-},isDirty:function(_68){
-if(_68){
-var _69=this.getIdentity(_68);
-return new Boolean(this._pending._newItems[_69]||this._pending._modifiedItems[_69]||this._pending._deletedItems[_69]).valueOf();
-}else{
-if(!this._isEmpty(this._pending._newItems)||!this._isEmpty(this._pending._modifiedItems)||!this._isEmpty(this._pending._deletedItems)){
-return true;
-}
-return false;
-}
-},onSet:function(_6a,_6b,_6c,_6d){
-},onNew:function(_6e,_6f){
-},onDelete:function(_70){
-},close:function(_71){
-if(this.clearOnClose){
-if(!this.isDirty()){
-this.inherited(arguments);
-}else{
-throw new Error("dojo.data.ItemFileWriteStore: There are unsaved changes present in the store.  Please save or revert the changes before invoking close.");
-}
-}
-}});
+
+dojo.declare("dojo.data.ItemFileWriteStore", dojo.data.ItemFileReadStore, {
+       constructor: function(/* object */ keywordParameters){
+               //      keywordParameters: {typeMap: object)
+               //              The structure of the typeMap object is as follows:
+               //              {
+               //                      type0: function || object,
+               //                      type1: function || object,
+               //                      ...
+               //                      typeN: function || object
+               //              }
+               //              Where if it is a function, it is assumed to be an object constructor that takes the 
+               //              value of _value as the initialization parameters.  It is serialized assuming object.toString()
+               //              serialization.  If it is an object, then it is assumed
+               //              to be an object of general form:
+               //              {
+               //                      type: function, //constructor.
+               //                      deserialize:    function(value) //The function that parses the value and constructs the object defined by type appropriately.
+               //                      serialize:      function(object) //The function that converts the object back into the proper file format form.
+               //              }
+
+               // ItemFileWriteStore extends ItemFileReadStore to implement these additional dojo.data APIs
+               this._features['dojo.data.api.Write'] = true;
+               this._features['dojo.data.api.Notification'] = true;
+               
+               // For keeping track of changes so that we can implement isDirty and revert
+               this._pending = {
+                       _newItems:{}, 
+                       _modifiedItems:{}, 
+                       _deletedItems:{}
+               };
+
+               if(!this._datatypeMap['Date'].serialize){
+                       this._datatypeMap['Date'].serialize = function(obj){
+                               return dojo.date.stamp.toISOString(obj, {zulu:true});
+                       };
+               }
+               //Disable only if explicitly set to false.
+               if(keywordParameters && (keywordParameters.referenceIntegrity === false)){
+                       this.referenceIntegrity = false;
+               }
+
+               // this._saveInProgress is set to true, briefly, from when save() is first called to when it completes
+               this._saveInProgress = false;
+       },
+
+       referenceIntegrity: true, //Flag that defaultly enabled reference integrity tracking.  This way it can also be disabled pogrammatially or declaratively.
+
+       _assert: function(/* boolean */ condition){
+               if(!condition){
+                       throw new Error("assertion failed in ItemFileWriteStore");
+               }
+       },
+
+       _getIdentifierAttribute: function(){
+               var identifierAttribute = this.getFeatures()['dojo.data.api.Identity'];
+               // this._assert((identifierAttribute === Number) || (dojo.isString(identifierAttribute)));
+               return identifierAttribute;
+       },
+       
+       
+/* dojo.data.api.Write */
+
+       newItem: function(/* Object? */ keywordArgs, /* Object? */ parentInfo){
+               // summary: See dojo.data.api.Write.newItem()
+
+               this._assert(!this._saveInProgress);
+
+               if(!this._loadFinished){
+                       // We need to do this here so that we'll be able to find out what
+                       // identifierAttribute was specified in the data file.
+                       this._forceLoad();
+               }
+
+               if(typeof keywordArgs != "object" && typeof keywordArgs != "undefined"){
+                       throw new Error("newItem() was passed something other than an object");
+               }
+               var newIdentity = null;
+               var identifierAttribute = this._getIdentifierAttribute();
+               if(identifierAttribute === Number){
+                       newIdentity = this._arrayOfAllItems.length;
+               }else{
+                       newIdentity = keywordArgs[identifierAttribute];
+                       if(typeof newIdentity === "undefined"){
+                               throw new Error("newItem() was not passed an identity for the new item");
+                       }
+                       if(dojo.isArray(newIdentity)){
+                               throw new Error("newItem() was not passed an single-valued identity");
+                       }
+               }
+               
+               // make sure this identity is not already in use by another item, if identifiers were 
+               // defined in the file.  Otherwise it would be the item count, 
+               // which should always be unique in this case.
+               if(this._itemsByIdentity){
+                       this._assert(typeof this._itemsByIdentity[newIdentity] === "undefined");
+               }
+               this._assert(typeof this._pending._newItems[newIdentity] === "undefined");
+               this._assert(typeof this._pending._deletedItems[newIdentity] === "undefined");
+               
+               var newItem = {};
+               newItem[this._storeRefPropName] = this;         
+               newItem[this._itemNumPropName] = this._arrayOfAllItems.length;
+               if(this._itemsByIdentity){
+                       this._itemsByIdentity[newIdentity] = newItem;
+                       //We have to set the identifier now, otherwise we can't look it
+                       //up at calls to setValueorValues in parentInfo handling.
+                       newItem[identifierAttribute] = [newIdentity];
+               }
+               this._arrayOfAllItems.push(newItem);
+
+               //We need to construct some data for the onNew call too...
+               var pInfo = null;
+               
+               // Now we need to check to see where we want to assign this thingm if any.
+               if(parentInfo && parentInfo.parent && parentInfo.attribute){
+                       pInfo = {
+                               item: parentInfo.parent,
+                               attribute: parentInfo.attribute,
+                               oldValue: undefined
+                       };
+
+                       //See if it is multi-valued or not and handle appropriately
+                       //Generally, all attributes are multi-valued for this store
+                       //So, we only need to append if there are already values present.
+                       var values = this.getValues(parentInfo.parent, parentInfo.attribute);
+                       if(values && values.length > 0){
+                               var tempValues = values.slice(0, values.length);
+                               if(values.length === 1){
+                                       pInfo.oldValue = values[0];
+                               }else{
+                                       pInfo.oldValue = values.slice(0, values.length);
+                               }
+                               tempValues.push(newItem);
+                               this._setValueOrValues(parentInfo.parent, parentInfo.attribute, tempValues, false);
+                               pInfo.newValue = this.getValues(parentInfo.parent, parentInfo.attribute);
+                       }else{
+                               this._setValueOrValues(parentInfo.parent, parentInfo.attribute, newItem, false);
+                               pInfo.newValue = newItem;
+                       }
+               }else{
+                       //Toplevel item, add to both top list as well as all list.
+                       newItem[this._rootItemPropName]=true;
+                       this._arrayOfTopLevelItems.push(newItem);
+               }
+               
+               this._pending._newItems[newIdentity] = newItem;
+               
+               //Clone over the properties to the new item
+               for(var key in keywordArgs){
+                       if(key === this._storeRefPropName || key === this._itemNumPropName){
+                               // Bummer, the user is trying to do something like
+                               // newItem({_S:"foo"}).  Unfortunately, our superclass,
+                               // ItemFileReadStore, is already using _S in each of our items
+                               // to hold private info.  To avoid a naming collision, we 
+                               // need to move all our private info to some other property 
+                               // of all the items/objects.  So, we need to iterate over all
+                               // the items and do something like: 
+                               //    item.__S = item._S;
+                               //    item._S = undefined;
+                               // But first we have to make sure the new "__S" variable is 
+                               // not in use, which means we have to iterate over all the 
+                               // items checking for that.
+                               throw new Error("encountered bug in ItemFileWriteStore.newItem");
+                       }
+                       var value = keywordArgs[key];
+                       if(!dojo.isArray(value)){
+                               value = [value];
+                       }
+                       newItem[key] = value;
+                       if(this.referenceIntegrity){
+                               for(var i = 0; i < value.length; i++){
+                                       var val = value[i];
+                                       if(this.isItem(val)){
+                                               this._addReferenceToMap(val, newItem, key);
+                                       }
+                               }
+                       }
+               }
+               this.onNew(newItem, pInfo); // dojo.data.api.Notification call
+               return newItem; // item
+       },
+       
+       _removeArrayElement: function(/* Array */ array, /* anything */ element){
+               var index = dojo.indexOf(array, element);
+               if(index != -1){
+                       array.splice(index, 1);
+                       return true;
+               }
+               return false;
+       },
+       
+       deleteItem: function(/* item */ item){
+               // summary: See dojo.data.api.Write.deleteItem()
+               this._assert(!this._saveInProgress);
+               this._assertIsItem(item);
+
+               // Remove this item from the _arrayOfAllItems, but leave a null value in place
+               // of the item, so as not to change the length of the array, so that in newItem() 
+               // we can still safely do: newIdentity = this._arrayOfAllItems.length;
+               var indexInArrayOfAllItems = item[this._itemNumPropName];
+               var identity = this.getIdentity(item);
+
+               //If we have reference integrity on, we need to do reference cleanup for the deleted item
+               if(this.referenceIntegrity){
+                       //First scan all the attributes of this items for references and clean them up in the map 
+                       //As this item is going away, no need to track its references anymore.
+
+                       //Get the attributes list before we generate the backup so it 
+                       //doesn't pollute the attributes list.
+                       var attributes = this.getAttributes(item);
+
+                       //Backup the map, we'll have to restore it potentially, in a revert.
+                       if(item[this._reverseRefMap]){
+                               item["backup_" + this._reverseRefMap] = dojo.clone(item[this._reverseRefMap]);
+                       }
+                       
+                       //TODO:  This causes a reversion problem.  This list won't be restored on revert since it is
+                       //attached to the 'value'. item, not ours.  Need to back tese up somehow too.
+                       //Maybe build a map of the backup of the entries and attach it to the deleted item to be restored
+                       //later.  Or just record them and call _addReferenceToMap on them in revert.
+                       dojo.forEach(attributes, function(attribute){
+                               dojo.forEach(this.getValues(item, attribute), function(value){
+                                       if(this.isItem(value)){
+                                               //We have to back up all the references we had to others so they can be restored on a revert.
+                                               if(!item["backupRefs_" + this._reverseRefMap]){
+                                                       item["backupRefs_" + this._reverseRefMap] = [];
+                                               }
+                                               item["backupRefs_" + this._reverseRefMap].push({id: this.getIdentity(value), attr: attribute});
+                                               this._removeReferenceFromMap(value, item, attribute);
+                                       }
+                               }, this);
+                       }, this);
+
+                       //Next, see if we have references to this item, if we do, we have to clean them up too.
+                       var references = item[this._reverseRefMap];
+                       if(references){
+                               //Look through all the items noted as references to clean them up.
+                               for(var itemId in references){
+                                       var containingItem = null;
+                                       if(this._itemsByIdentity){
+                                               containingItem = this._itemsByIdentity[itemId];
+                                       }else{
+                                               containingItem = this._arrayOfAllItems[itemId];
+                                       }
+                                       //We have a reference to a containing item, now we have to process the
+                                       //attributes and clear all references to the item being deleted.
+                                       if(containingItem){
+                                               for(var attribute in references[itemId]){
+                                                       var oldValues = this.getValues(containingItem, attribute) || [];
+                                                       var newValues = dojo.filter(oldValues, function(possibleItem){
+                                                               return !(this.isItem(possibleItem) && this.getIdentity(possibleItem) == identity);
+                                                       }, this);
+                                                       //Remove the note of the reference to the item and set the values on the modified attribute.
+                                                       this._removeReferenceFromMap(item, containingItem, attribute); 
+                                                       if(newValues.length < oldValues.length){
+                                                               this._setValueOrValues(containingItem, attribute, newValues, true);
+                                                       }
+                                               }
+                                       }
+                               }
+                       }
+               }
+
+               this._arrayOfAllItems[indexInArrayOfAllItems] = null;
+
+               item[this._storeRefPropName] = null;
+               if(this._itemsByIdentity){
+                       delete this._itemsByIdentity[identity];
+               }
+               this._pending._deletedItems[identity] = item;
+               
+               //Remove from the toplevel items, if necessary...
+               if(item[this._rootItemPropName]){
+                       this._removeArrayElement(this._arrayOfTopLevelItems, item);
+               }
+               this.onDelete(item); // dojo.data.api.Notification call
+               return true;
+       },
+
+       setValue: function(/* item */ item, /* attribute-name-string */ attribute, /* almost anything */ value){
+               // summary: See dojo.data.api.Write.set()
+               return this._setValueOrValues(item, attribute, value, true); // boolean
+       },
+       
+       setValues: function(/* item */ item, /* attribute-name-string */ attribute, /* array */ values){
+               // summary: See dojo.data.api.Write.setValues()
+               return this._setValueOrValues(item, attribute, values, true); // boolean
+       },
+       
+       unsetAttribute: function(/* item */ item, /* attribute-name-string */ attribute){
+               // summary: See dojo.data.api.Write.unsetAttribute()
+               return this._setValueOrValues(item, attribute, [], true);
+       },
+       
+       _setValueOrValues: function(/* item */ item, /* attribute-name-string */ attribute, /* anything */ newValueOrValues, /*boolean?*/ callOnSet){
+               this._assert(!this._saveInProgress);
+               
+               // Check for valid arguments
+               this._assertIsItem(item);
+               this._assert(dojo.isString(attribute));
+               this._assert(typeof newValueOrValues !== "undefined");
+
+               // Make sure the user isn't trying to change the item's identity
+               var identifierAttribute = this._getIdentifierAttribute();
+               if(attribute == identifierAttribute){
+                       throw new Error("ItemFileWriteStore does not have support for changing the value of an item's identifier.");
+               }
+
+               // To implement the Notification API, we need to make a note of what
+               // the old attribute value was, so that we can pass that info when
+               // we call the onSet method.
+               var oldValueOrValues = this._getValueOrValues(item, attribute);
+
+               var identity = this.getIdentity(item);
+               if(!this._pending._modifiedItems[identity]){
+                       // Before we actually change the item, we make a copy of it to 
+                       // record the original state, so that we'll be able to revert if 
+                       // the revert method gets called.  If the item has already been
+                       // modified then there's no need to do this now, since we already
+                       // have a record of the original state.                                         
+                       var copyOfItemState = {};
+                       for(var key in item){
+                               if((key === this._storeRefPropName) || (key === this._itemNumPropName) || (key === this._rootItemPropName)){
+                                       copyOfItemState[key] = item[key];
+                               }else if(key === this._reverseRefMap){
+                                       copyOfItemState[key] = dojo.clone(item[key]);
+                               }else{
+                                       copyOfItemState[key] = item[key].slice(0, item[key].length);
+                               }
+                       }
+                       // Now mark the item as dirty, and save the copy of the original state
+                       this._pending._modifiedItems[identity] = copyOfItemState;
+               }
+               
+               // Okay, now we can actually change this attribute on the item
+               var success = false;
+               
+               if(dojo.isArray(newValueOrValues) && newValueOrValues.length === 0){
+                       
+                       // If we were passed an empty array as the value, that counts
+                       // as "unsetting" the attribute, so we need to remove this 
+                       // attribute from the item.
+                       success = delete item[attribute];
+                       newValueOrValues = undefined; // used in the onSet Notification call below
+
+                       if(this.referenceIntegrity && oldValueOrValues){
+                               var oldValues = oldValueOrValues;
+                               if(!dojo.isArray(oldValues)){
+                                       oldValues = [oldValues];
+                               }
+                               for(var i = 0; i < oldValues.length; i++){
+                                       var value = oldValues[i];
+                                       if(this.isItem(value)){
+                                               this._removeReferenceFromMap(value, item, attribute);
+                                       }
+                               }
+                       }
+               }else{
+                       var newValueArray;
+                       if(dojo.isArray(newValueOrValues)){
+                               var newValues = newValueOrValues;
+                               // Unfortunately, it's not safe to just do this:
+                               //    newValueArray = newValues;
+                               // Instead, we need to copy the array, which slice() does very nicely.
+                               // This is so that our internal data structure won't  
+                               // get corrupted if the user mucks with the values array *after*
+                               // calling setValues().
+                               newValueArray = newValueOrValues.slice(0, newValueOrValues.length);
+                       }else{
+                               newValueArray = [newValueOrValues];
+                       }
+
+                       //We need to handle reference integrity if this is on. 
+                       //In the case of set, we need to see if references were added or removed
+                       //and update the reference tracking map accordingly.
+                       if(this.referenceIntegrity){
+                               if(oldValueOrValues){
+                                       var oldValues = oldValueOrValues;
+                                       if(!dojo.isArray(oldValues)){
+                                               oldValues = [oldValues];
+                                       }
+                                       //Use an associative map to determine what was added/removed from the list.
+                                       //Should be O(n) performant.  First look at all the old values and make a list of them
+                                       //Then for any item not in the old list, we add it.  If it was already present, we remove it.
+                                       //Then we pass over the map and any references left it it need to be removed (IE, no match in
+                                       //the new values list).
+                                       var map = {};
+                                       dojo.forEach(oldValues, function(possibleItem){
+                                               if(this.isItem(possibleItem)){
+                                                       var id = this.getIdentity(possibleItem);
+                                                       map[id.toString()] = true;
+                                               }
+                                       }, this);
+                                       dojo.forEach(newValueArray, function(possibleItem){
+                                               if(this.isItem(possibleItem)){
+                                                       var id = this.getIdentity(possibleItem);
+                                                       if(map[id.toString()]){
+                                                               delete map[id.toString()];
+                                                       }else{
+                                                               this._addReferenceToMap(possibleItem, item, attribute); 
+                                                       }
+                                               }
+                                       }, this);
+                                       for(var rId in map){
+                                               var removedItem;
+                                               if(this._itemsByIdentity){
+                                                       removedItem = this._itemsByIdentity[rId];
+                                               }else{
+                                                       removedItem = this._arrayOfAllItems[rId];
+                                               }
+                                               this._removeReferenceFromMap(removedItem, item, attribute);
+                                       }
+                               }else{
+                                       //Everything is new (no old values) so we have to just
+                                       //insert all the references, if any.
+                                       for(var i = 0; i < newValueArray.length; i++){
+                                               var value = newValueArray[i];
+                                               if(this.isItem(value)){
+                                                       this._addReferenceToMap(value, item, attribute);
+                                               }
+                                       }
+                               }
+                       }
+                       item[attribute] = newValueArray;
+                       success = true;
+               }
+
+               // Now we make the dojo.data.api.Notification call
+               if(callOnSet){
+                       this.onSet(item, attribute, oldValueOrValues, newValueOrValues); 
+               }
+               return success; // boolean
+       },
+
+       _addReferenceToMap: function(/*item*/ refItem, /*item*/ parentItem, /*string*/ attribute){
+               //      summary:
+               //              Method to add an reference map entry for an item and attribute.
+               //      description:
+               //              Method to add an reference map entry for an item and attribute.                  //
+               //      refItem:
+               //              The item that is referenced.
+               //      parentItem:
+               //              The item that holds the new reference to refItem.
+               //      attribute:
+               //              The attribute on parentItem that contains the new reference.
+                
+               var parentId = this.getIdentity(parentItem);
+               var references = refItem[this._reverseRefMap];
+
+               if(!references){
+                       references = refItem[this._reverseRefMap] = {};
+               }
+               var itemRef = references[parentId];
+               if(!itemRef){
+                       itemRef = references[parentId] = {};
+               }
+               itemRef[attribute] = true;
+       },
+
+       _removeReferenceFromMap: function(/* item */ refItem, /* item */ parentItem, /*strin*/ attribute){
+               //      summary:
+               //              Method to remove an reference map entry for an item and attribute.
+               //      description:
+               //              Method to remove an reference map entry for an item and attribute.  This will
+               //              also perform cleanup on the map such that if there are no more references at all to 
+               //              the item, its reference object and entry are removed.
+               //
+               //      refItem:
+               //              The item that is referenced.
+               //      parentItem:
+               //              The item holding a reference to refItem.
+               //      attribute:
+               //              The attribute on parentItem that contains the reference.
+               var identity = this.getIdentity(parentItem);
+               var references = refItem[this._reverseRefMap];
+               var itemId;
+               if(references){
+                       for(itemId in references){
+                               if(itemId == identity){
+                                       delete references[itemId][attribute];
+                                       if(this._isEmpty(references[itemId])){
+                                               delete references[itemId];
+                                       }
+                               }
+                       }
+                       if(this._isEmpty(references)){
+                               delete refItem[this._reverseRefMap];
+                       }
+               }
+       },
+
+       _dumpReferenceMap: function(){
+               //      summary:
+               //              Function to dump the reverse reference map of all items in the store for debug purposes.
+               //      description:
+               //              Function to dump the reverse reference map of all items in the store for debug purposes.
+               var i;
+               for(i = 0; i < this._arrayOfAllItems.length; i++){
+                       var item = this._arrayOfAllItems[i];
+                       if(item && item[this._reverseRefMap]){
+                               console.log("Item: [" + this.getIdentity(item) + "] is referenced by: " + dojo.toJson(item[this._reverseRefMap]));
+                       }
+               }
+       },
+       
+       _getValueOrValues: function(/* item */ item, /* attribute-name-string */ attribute){
+               var valueOrValues = undefined;
+               if(this.hasAttribute(item, attribute)){
+                       var valueArray = this.getValues(item, attribute);
+                       if(valueArray.length == 1){
+                               valueOrValues = valueArray[0];
+                       }else{
+                               valueOrValues = valueArray;
+                       }
+               }
+               return valueOrValues;
+       },
+       
+       _flatten: function(/* anything */ value){
+               if(this.isItem(value)){
+                       var item = value;
+                       // Given an item, return an serializable object that provides a 
+                       // reference to the item.
+                       // For example, given kermit:
+                       //    var kermit = store.newItem({id:2, name:"Kermit"});
+                       // we want to return
+                       //    {_reference:2}
+                       var identity = this.getIdentity(item);
+                       var referenceObject = {_reference: identity};
+                       return referenceObject;
+               }else{
+                       if(typeof value === "object"){
+                               for(var type in this._datatypeMap){
+                                       var typeMap = this._datatypeMap[type];
+                                       if(dojo.isObject(typeMap) && !dojo.isFunction(typeMap)){
+                                               if(value instanceof typeMap.type){
+                                                       if(!typeMap.serialize){
+                                                               throw new Error("ItemFileWriteStore:  No serializer defined for type mapping: [" + type + "]");
+                                                       }
+                                                       return {_type: type, _value: typeMap.serialize(value)};
+                                               }
+                                       } else if(value instanceof typeMap){
+                                               //SImple mapping, therefore, return as a toString serialization.
+                                               return {_type: type, _value: value.toString()};
+                                       }
+                               }
+                       }
+                       return value;
+               }
+       },
+       
+       _getNewFileContentString: function(){
+               // summary: 
+               //              Generate a string that can be saved to a file.
+               //              The result should look similar to:
+               //              http://trac.dojotoolkit.org/browser/dojo/trunk/tests/data/countries.json
+               var serializableStructure = {};
+               
+               var identifierAttribute = this._getIdentifierAttribute();
+               if(identifierAttribute !== Number){
+                       serializableStructure.identifier = identifierAttribute;
+               }
+               if(this._labelAttr){
+                       serializableStructure.label = this._labelAttr;
+               }
+               serializableStructure.items = [];
+               for(var i = 0; i < this._arrayOfAllItems.length; ++i){
+                       var item = this._arrayOfAllItems[i];
+                       if(item !== null){
+                               var serializableItem = {};
+                               for(var key in item){
+                                       if(key !== this._storeRefPropName && key !== this._itemNumPropName && key !== this._reverseRefMap && key !== this._rootItemPropName){
+                                               var attribute = key;
+                                               var valueArray = this.getValues(item, attribute);
+                                               if(valueArray.length == 1){
+                                                       serializableItem[attribute] = this._flatten(valueArray[0]);
+                                               }else{
+                                                       var serializableArray = [];
+                                                       for(var j = 0; j < valueArray.length; ++j){
+                                                               serializableArray.push(this._flatten(valueArray[j]));
+                                                               serializableItem[attribute] = serializableArray;
+                                                       }
+                                               }
+                                       }
+                               }
+                               serializableStructure.items.push(serializableItem);
+                       }
+               }
+               var prettyPrint = true;
+               return dojo.toJson(serializableStructure, prettyPrint);
+       },
+
+       _isEmpty: function(something){
+               //      summary: 
+               //              Function to determine if an array or object has no properties or values.
+               //      something:
+               //              The array or object to examine.
+               var empty = true;
+               if(dojo.isObject(something)){
+                       var i;
+                       for(i in something){
+                               empty = false;
+                               break;
+                       }
+               }else if(dojo.isArray(something)){
+                       if(something.length > 0){
+                               empty = false;
+                       }
+               }
+               return empty; //boolean
+       },
+       
+       save: function(/* object */ keywordArgs){
+               // summary: See dojo.data.api.Write.save()
+               this._assert(!this._saveInProgress);
+               
+               // this._saveInProgress is set to true, briefly, from when save is first called to when it completes
+               this._saveInProgress = true;
+               
+               var self = this;
+               var saveCompleteCallback = function(){
+                       self._pending = {
+                               _newItems:{}, 
+                               _modifiedItems:{},
+                               _deletedItems:{}
+                       };
+
+                       self._saveInProgress = false; // must come after this._pending is cleared, but before any callbacks
+                       if(keywordArgs && keywordArgs.onComplete){
+                               var scope = keywordArgs.scope || dojo.global;
+                               keywordArgs.onComplete.call(scope);
+                       }
+               };
+               var saveFailedCallback = function(err){
+                       self._saveInProgress = false;
+                       if(keywordArgs && keywordArgs.onError){
+                               var scope = keywordArgs.scope || dojo.global;
+                               keywordArgs.onError.call(scope, err);
+                       }
+               };
+               
+               if(this._saveEverything){
+                       var newFileContentString = this._getNewFileContentString();
+                       this._saveEverything(saveCompleteCallback, saveFailedCallback, newFileContentString);
+               }
+               if(this._saveCustom){
+                       this._saveCustom(saveCompleteCallback, saveFailedCallback);
+               }
+               if(!this._saveEverything && !this._saveCustom){
+                       // Looks like there is no user-defined save-handler function.
+                       // That's fine, it just means the datastore is acting as a "mock-write"
+                       // store -- changes get saved in memory but don't get saved to disk.
+                       saveCompleteCallback();
+               }
+       },
+       
+       revert: function(){
+               // summary: See dojo.data.api.Write.revert()
+               this._assert(!this._saveInProgress);
+
+               var identity;
+               for(identity in this._pending._modifiedItems){
+                       // find the original item and the modified item that replaced it
+                       var copyOfItemState = this._pending._modifiedItems[identity];
+                       var modifiedItem = null;
+                       if(this._itemsByIdentity){
+                               modifiedItem = this._itemsByIdentity[identity];
+                       }else{
+                               modifiedItem = this._arrayOfAllItems[identity];
+                       }
+       
+                       // Restore the original item into a full-fledged item again, we want to try to 
+                       // keep the same object instance as if we don't it, causes bugs like #9022.
+                       copyOfItemState[this._storeRefPropName] = this;
+                       for(key in modifiedItem){
+                               delete modifiedItem[key];
+                       }
+                       dojo.mixin(modifiedItem, copyOfItemState);
+               }
+               var deletedItem;
+               for(identity in this._pending._deletedItems){
+                       deletedItem = this._pending._deletedItems[identity];
+                       deletedItem[this._storeRefPropName] = this;
+                       var index = deletedItem[this._itemNumPropName];
+
+                       //Restore the reverse refererence map, if any.
+                       if(deletedItem["backup_" + this._reverseRefMap]){
+                               deletedItem[this._reverseRefMap] = deletedItem["backup_" + this._reverseRefMap];
+                               delete deletedItem["backup_" + this._reverseRefMap];
+                       }
+                       this._arrayOfAllItems[index] = deletedItem;
+                       if(this._itemsByIdentity){
+                               this._itemsByIdentity[identity] = deletedItem;
+                       }
+                       if(deletedItem[this._rootItemPropName]){
+                               this._arrayOfTopLevelItems.push(deletedItem);
+                       }
+               }
+               //We have to pass through it again and restore the reference maps after all the
+               //undeletes have occurred.
+               for(identity in this._pending._deletedItems){
+                       deletedItem = this._pending._deletedItems[identity];
+                       if(deletedItem["backupRefs_" + this._reverseRefMap]){
+                               dojo.forEach(deletedItem["backupRefs_" + this._reverseRefMap], function(reference){
+                                       var refItem;
+                                       if(this._itemsByIdentity){
+                                               refItem = this._itemsByIdentity[reference.id];
+                                       }else{
+                                               refItem = this._arrayOfAllItems[reference.id];
+                                       }
+                                       this._addReferenceToMap(refItem, deletedItem, reference.attr);
+                               }, this);
+                               delete deletedItem["backupRefs_" + this._reverseRefMap]; 
+                       }
+               }
+
+               for(identity in this._pending._newItems){
+                       var newItem = this._pending._newItems[identity];
+                       newItem[this._storeRefPropName] = null;
+                       // null out the new item, but don't change the array index so
+                       // so we can keep using _arrayOfAllItems.length.
+                       this._arrayOfAllItems[newItem[this._itemNumPropName]] = null;
+                       if(newItem[this._rootItemPropName]){
+                               this._removeArrayElement(this._arrayOfTopLevelItems, newItem);
+                       }
+                       if(this._itemsByIdentity){
+                               delete this._itemsByIdentity[identity];
+                       }
+               }
+
+               this._pending = {
+                       _newItems:{}, 
+                       _modifiedItems:{}, 
+                       _deletedItems:{}
+               };
+               return true; // boolean
+       },
+       
+       isDirty: function(/* item? */ item){
+               // summary: See dojo.data.api.Write.isDirty()
+               if(item){
+                       // return true if the item is dirty
+                       var identity = this.getIdentity(item);
+                       return new Boolean(this._pending._newItems[identity] || 
+                               this._pending._modifiedItems[identity] ||
+                               this._pending._deletedItems[identity]).valueOf(); // boolean
+               }else{
+                       // return true if the store is dirty -- which means return true
+                       // if there are any new items, dirty items, or modified items
+                       if(!this._isEmpty(this._pending._newItems) || 
+                               !this._isEmpty(this._pending._modifiedItems) ||
+                               !this._isEmpty(this._pending._deletedItems)){
+                               return true;
+                       }
+                       return false; // boolean
+               }
+       },
+
+/* dojo.data.api.Notification */
+
+       onSet: function(/* item */ item, 
+                                       /*attribute-name-string*/ attribute, 
+                                       /*object | array*/ oldValue,
+                                       /*object | array*/ newValue){
+               // summary: See dojo.data.api.Notification.onSet()
+               
+               // No need to do anything. This method is here just so that the 
+               // client code can connect observers to it.
+       },
+
+       onNew: function(/* item */ newItem, /*object?*/ parentInfo){
+               // summary: See dojo.data.api.Notification.onNew()
+               
+               // No need to do anything. This method is here just so that the 
+               // client code can connect observers to it. 
+       },
+
+       onDelete: function(/* item */ deletedItem){
+               // summary: See dojo.data.api.Notification.onDelete()
+               
+               // No need to do anything. This method is here just so that the 
+               // client code can connect observers to it. 
+       },
+
+       close: function(/* object? */ request){
+                // summary:
+                //             Over-ride of base close function of ItemFileReadStore to add in check for store state.
+                // description:
+                //             Over-ride of base close function of ItemFileReadStore to add in check for store state.
+                //             If the store is still dirty (unsaved changes), then an error will be thrown instead of
+                //             clearing the internal state for reload from the url.
+
+                //Clear if not dirty ... or throw an error
+                if(this.clearOnClose){
+                        if(!this.isDirty()){
+                                this.inherited(arguments);
+                        }else{
+                                //Only throw an error if the store was dirty and we were loading from a url (cannot reload from url until state is saved).
+                                throw new Error("dojo.data.ItemFileWriteStore: There are unsaved changes present in the store.  Please save or revert the changes before invoking close.");
+                        }
+                }
+       }
+});
+
 }