dojo.data.ItemFileWriteStore._setValueOrValues
dojo.require("dojo.data.ItemFileWriteStore");
defined in dojo/data/ItemFileWriteStore.js
Usage
function (item, attribute, newValueOrValues, /*Boolean?*/ callOnSet) (view source)
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
parameter | type | description |
---|---|---|
item | ||
attribute | ||
newValueOrValues | ||
callOnSet | Boolean | Optional. |