dojo.data.ItemFileWriteStore.newItem
dojo.require("dojo.data.ItemFileWriteStore");
defined in dojo/data/ItemFileWriteStore.js
See dojo.data.api.Write.newItem()
Usage
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
parameter | type | description |
---|---|---|
keywordArgs | Object | Optional. |
parentInfo | Object | Optional. |