AndOrWriteStore.js 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819
  1. /*
  2. Copyright (c) 2004-2012, The Dojo Foundation All Rights Reserved.
  3. Available via Academic Free License >= 2.1 OR the modified BSD license.
  4. see: http://dojotoolkit.org/license for details
  5. */
  6. if(!dojo._hasResource["dojox.data.AndOrWriteStore"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
  7. dojo._hasResource["dojox.data.AndOrWriteStore"] = true;
  8. dojo.provide("dojox.data.AndOrWriteStore");
  9. dojo.require("dojox.data.AndOrReadStore");
  10. dojo.declare("dojox.data.AndOrWriteStore", dojox.data.AndOrReadStore, {
  11. constructor: function(/* object */ keywordParameters){
  12. // keywordParameters: {typeMap: object)
  13. // The structure of the typeMap object is as follows:
  14. // {
  15. // type0: function || object,
  16. // type1: function || object,
  17. // ...
  18. // typeN: function || object
  19. // }
  20. // Where if it is a function, it is assumed to be an object constructor that takes the
  21. // value of _value as the initialization parameters. It is serialized assuming object.toString()
  22. // serialization. If it is an object, then it is assumed
  23. // to be an object of general form:
  24. // {
  25. // type: function, //constructor.
  26. // deserialize: function(value) //The function that parses the value and constructs the object defined by type appropriately.
  27. // serialize: function(object) //The function that converts the object back into the proper file format form.
  28. // }
  29. // AndOrWriteStore duplicates ItemFileWriteStore, except extends AndOrReadStore, which offers complex queries.
  30. // ItemFileWriteStore extends ItemFileReadStore to implement these additional dojo.data APIs
  31. this._features['dojo.data.api.Write'] = true;
  32. this._features['dojo.data.api.Notification'] = true;
  33. // For keeping track of changes so that we can implement isDirty and revert
  34. this._pending = {
  35. _newItems:{},
  36. _modifiedItems:{},
  37. _deletedItems:{}
  38. };
  39. if(!this._datatypeMap['Date'].serialize){
  40. this._datatypeMap['Date'].serialize = function(obj){
  41. return dojo.date.stamp.toISOString(obj, {zulu:true});
  42. };
  43. }
  44. //Disable only if explicitly set to false.
  45. if(keywordParameters && (keywordParameters.referenceIntegrity === false)){
  46. this.referenceIntegrity = false;
  47. }
  48. // this._saveInProgress is set to true, briefly, from when save() is first called to when it completes
  49. this._saveInProgress = false;
  50. },
  51. referenceIntegrity: true, //Flag that defaultly enabled reference integrity tracking. This way it can also be disabled pogrammatially or declaratively.
  52. _assert: function(/* boolean */ condition){
  53. if(!condition){
  54. throw new Error("assertion failed in ItemFileWriteStore");
  55. }
  56. },
  57. _getIdentifierAttribute: function(){
  58. var identifierAttribute = this.getFeatures()['dojo.data.api.Identity'];
  59. // this._assert((identifierAttribute === Number) || (dojo.isString(identifierAttribute)));
  60. return identifierAttribute;
  61. },
  62. /* dojo.data.api.Write */
  63. newItem: function(/* Object? */ keywordArgs, /* Object? */ parentInfo){
  64. // summary: See dojo.data.api.Write.newItem()
  65. this._assert(!this._saveInProgress);
  66. if(!this._loadFinished){
  67. // We need to do this here so that we'll be able to find out what
  68. // identifierAttribute was specified in the data file.
  69. this._forceLoad();
  70. }
  71. if(typeof keywordArgs != "object" && typeof keywordArgs != "undefined"){
  72. throw new Error("newItem() was passed something other than an object");
  73. }
  74. var newIdentity = null;
  75. var identifierAttribute = this._getIdentifierAttribute();
  76. if(identifierAttribute === Number){
  77. newIdentity = this._arrayOfAllItems.length;
  78. }else{
  79. newIdentity = keywordArgs[identifierAttribute];
  80. if(typeof newIdentity === "undefined"){
  81. throw new Error("newItem() was not passed an identity for the new item");
  82. }
  83. if(dojo.isArray(newIdentity)){
  84. throw new Error("newItem() was not passed an single-valued identity");
  85. }
  86. }
  87. // make sure this identity is not already in use by another item, if identifiers were
  88. // defined in the file. Otherwise it would be the item count,
  89. // which should always be unique in this case.
  90. if(this._itemsByIdentity){
  91. this._assert(typeof this._itemsByIdentity[newIdentity] === "undefined");
  92. }
  93. this._assert(typeof this._pending._newItems[newIdentity] === "undefined");
  94. this._assert(typeof this._pending._deletedItems[newIdentity] === "undefined");
  95. var newItem = {};
  96. newItem[this._storeRefPropName] = this;
  97. newItem[this._itemNumPropName] = this._arrayOfAllItems.length;
  98. if(this._itemsByIdentity){
  99. this._itemsByIdentity[newIdentity] = newItem;
  100. //We have to set the identifier now, otherwise we can't look it
  101. //up at calls to setValueorValues in parentInfo handling.
  102. newItem[identifierAttribute] = [newIdentity];
  103. }
  104. this._arrayOfAllItems.push(newItem);
  105. //We need to construct some data for the onNew call too...
  106. var pInfo = null;
  107. // Now we need to check to see where we want to assign this thingm if any.
  108. if(parentInfo && parentInfo.parent && parentInfo.attribute){
  109. pInfo = {
  110. item: parentInfo.parent,
  111. attribute: parentInfo.attribute,
  112. oldValue: undefined
  113. };
  114. //See if it is multi-valued or not and handle appropriately
  115. //Generally, all attributes are multi-valued for this store
  116. //So, we only need to append if there are already values present.
  117. var values = this.getValues(parentInfo.parent, parentInfo.attribute);
  118. if(values && values.length > 0){
  119. var tempValues = values.slice(0, values.length);
  120. if(values.length === 1){
  121. pInfo.oldValue = values[0];
  122. }else{
  123. pInfo.oldValue = values.slice(0, values.length);
  124. }
  125. tempValues.push(newItem);
  126. this._setValueOrValues(parentInfo.parent, parentInfo.attribute, tempValues, false);
  127. pInfo.newValue = this.getValues(parentInfo.parent, parentInfo.attribute);
  128. }else{
  129. this._setValueOrValues(parentInfo.parent, parentInfo.attribute, newItem, false);
  130. pInfo.newValue = newItem;
  131. }
  132. }else{
  133. //Toplevel item, add to both top list as well as all list.
  134. newItem[this._rootItemPropName]=true;
  135. this._arrayOfTopLevelItems.push(newItem);
  136. }
  137. this._pending._newItems[newIdentity] = newItem;
  138. //Clone over the properties to the new item
  139. for(var key in keywordArgs){
  140. if(key === this._storeRefPropName || key === this._itemNumPropName){
  141. // Bummer, the user is trying to do something like
  142. // newItem({_S:"foo"}). Unfortunately, our superclass,
  143. // ItemFileReadStore, is already using _S in each of our items
  144. // to hold private info. To avoid a naming collision, we
  145. // need to move all our private info to some other property
  146. // of all the items/objects. So, we need to iterate over all
  147. // the items and do something like:
  148. // item.__S = item._S;
  149. // item._S = undefined;
  150. // But first we have to make sure the new "__S" variable is
  151. // not in use, which means we have to iterate over all the
  152. // items checking for that.
  153. throw new Error("encountered bug in ItemFileWriteStore.newItem");
  154. }
  155. var value = keywordArgs[key];
  156. if(!dojo.isArray(value)){
  157. value = [value];
  158. }
  159. newItem[key] = value;
  160. if(this.referenceIntegrity){
  161. for(var i = 0; i < value.length; i++){
  162. var val = value[i];
  163. if(this.isItem(val)){
  164. this._addReferenceToMap(val, newItem, key);
  165. }
  166. }
  167. }
  168. }
  169. this.onNew(newItem, pInfo); // dojo.data.api.Notification call
  170. return newItem; // item
  171. },
  172. _removeArrayElement: function(/* Array */ array, /* anything */ element){
  173. var index = dojo.indexOf(array, element);
  174. if(index != -1){
  175. array.splice(index, 1);
  176. return true;
  177. }
  178. return false;
  179. },
  180. deleteItem: function(/* item */ item){
  181. // summary: See dojo.data.api.Write.deleteItem()
  182. this._assert(!this._saveInProgress);
  183. this._assertIsItem(item);
  184. // Remove this item from the _arrayOfAllItems, but leave a null value in place
  185. // of the item, so as not to change the length of the array, so that in newItem()
  186. // we can still safely do: newIdentity = this._arrayOfAllItems.length;
  187. var indexInArrayOfAllItems = item[this._itemNumPropName];
  188. var identity = this.getIdentity(item);
  189. //If we have reference integrity on, we need to do reference cleanup for the deleted item
  190. if(this.referenceIntegrity){
  191. //First scan all the attributes of this items for references and clean them up in the map
  192. //As this item is going away, no need to track its references anymore.
  193. //Get the attributes list before we generate the backup so it
  194. //doesn't pollute the attributes list.
  195. var attributes = this.getAttributes(item);
  196. //Backup the map, we'll have to restore it potentially, in a revert.
  197. if(item[this._reverseRefMap]){
  198. item["backup_" + this._reverseRefMap] = dojo.clone(item[this._reverseRefMap]);
  199. }
  200. //TODO: This causes a reversion problem. This list won't be restored on revert since it is
  201. //attached to the 'value'. item, not ours. Need to back tese up somehow too.
  202. //Maybe build a map of the backup of the entries and attach it to the deleted item to be restored
  203. //later. Or just record them and call _addReferenceToMap on them in revert.
  204. dojo.forEach(attributes, function(attribute){
  205. dojo.forEach(this.getValues(item, attribute), function(value){
  206. if(this.isItem(value)){
  207. //We have to back up all the references we had to others so they can be restored on a revert.
  208. if(!item["backupRefs_" + this._reverseRefMap]){
  209. item["backupRefs_" + this._reverseRefMap] = [];
  210. }
  211. item["backupRefs_" + this._reverseRefMap].push({id: this.getIdentity(value), attr: attribute});
  212. this._removeReferenceFromMap(value, item, attribute);
  213. }
  214. }, this);
  215. }, this);
  216. //Next, see if we have references to this item, if we do, we have to clean them up too.
  217. var references = item[this._reverseRefMap];
  218. if(references){
  219. //Look through all the items noted as references to clean them up.
  220. for(var itemId in references){
  221. var containingItem = null;
  222. if(this._itemsByIdentity){
  223. containingItem = this._itemsByIdentity[itemId];
  224. }else{
  225. containingItem = this._arrayOfAllItems[itemId];
  226. }
  227. //We have a reference to a containing item, now we have to process the
  228. //attributes and clear all references to the item being deleted.
  229. if(containingItem){
  230. for(var attribute in references[itemId]){
  231. var oldValues = this.getValues(containingItem, attribute) || [];
  232. var newValues = dojo.filter(oldValues, function(possibleItem){
  233. return !(this.isItem(possibleItem) && this.getIdentity(possibleItem) == identity);
  234. }, this);
  235. //Remove the note of the reference to the item and set the values on the modified attribute.
  236. this._removeReferenceFromMap(item, containingItem, attribute);
  237. if(newValues.length < oldValues.length){
  238. this._setValueOrValues(containingItem, attribute, newValues);
  239. }
  240. }
  241. }
  242. }
  243. }
  244. }
  245. this._arrayOfAllItems[indexInArrayOfAllItems] = null;
  246. item[this._storeRefPropName] = null;
  247. if(this._itemsByIdentity){
  248. delete this._itemsByIdentity[identity];
  249. }
  250. this._pending._deletedItems[identity] = item;
  251. //Remove from the toplevel items, if necessary...
  252. if(item[this._rootItemPropName]){
  253. this._removeArrayElement(this._arrayOfTopLevelItems, item);
  254. }
  255. this.onDelete(item); // dojo.data.api.Notification call
  256. return true;
  257. },
  258. setValue: function(/* item */ item, /* attribute-name-string */ attribute, /* almost anything */ value){
  259. // summary: See dojo.data.api.Write.set()
  260. return this._setValueOrValues(item, attribute, value, true); // boolean
  261. },
  262. setValues: function(/* item */ item, /* attribute-name-string */ attribute, /* array */ values){
  263. // summary: See dojo.data.api.Write.setValues()
  264. return this._setValueOrValues(item, attribute, values, true); // boolean
  265. },
  266. unsetAttribute: function(/* item */ item, /* attribute-name-string */ attribute){
  267. // summary: See dojo.data.api.Write.unsetAttribute()
  268. return this._setValueOrValues(item, attribute, [], true);
  269. },
  270. _setValueOrValues: function(/* item */ item, /* attribute-name-string */ attribute, /* anything */ newValueOrValues, /*boolean?*/ callOnSet){
  271. this._assert(!this._saveInProgress);
  272. // Check for valid arguments
  273. this._assertIsItem(item);
  274. this._assert(dojo.isString(attribute));
  275. this._assert(typeof newValueOrValues !== "undefined");
  276. // Make sure the user isn't trying to change the item's identity
  277. var identifierAttribute = this._getIdentifierAttribute();
  278. if(attribute == identifierAttribute){
  279. throw new Error("ItemFileWriteStore does not have support for changing the value of an item's identifier.");
  280. }
  281. // To implement the Notification API, we need to make a note of what
  282. // the old attribute value was, so that we can pass that info when
  283. // we call the onSet method.
  284. var oldValueOrValues = this._getValueOrValues(item, attribute);
  285. var identity = this.getIdentity(item);
  286. if(!this._pending._modifiedItems[identity]){
  287. // Before we actually change the item, we make a copy of it to
  288. // record the original state, so that we'll be able to revert if
  289. // the revert method gets called. If the item has already been
  290. // modified then there's no need to do this now, since we already
  291. // have a record of the original state.
  292. var copyOfItemState = {};
  293. for(var key in item){
  294. if((key === this._storeRefPropName) || (key === this._itemNumPropName) || (key === this._rootItemPropName)){
  295. copyOfItemState[key] = item[key];
  296. }else if(key === this._reverseRefMap){
  297. copyOfItemState[key] = dojo.clone(item[key]);
  298. }else{
  299. copyOfItemState[key] = item[key].slice(0, item[key].length);
  300. }
  301. }
  302. // Now mark the item as dirty, and save the copy of the original state
  303. this._pending._modifiedItems[identity] = copyOfItemState;
  304. }
  305. // Okay, now we can actually change this attribute on the item
  306. var success = false;
  307. if(dojo.isArray(newValueOrValues) && newValueOrValues.length === 0){
  308. // If we were passed an empty array as the value, that counts
  309. // as "unsetting" the attribute, so we need to remove this
  310. // attribute from the item.
  311. success = delete item[attribute];
  312. newValueOrValues = undefined; // used in the onSet Notification call below
  313. if(this.referenceIntegrity && oldValueOrValues){
  314. var oldValues = oldValueOrValues;
  315. if(!dojo.isArray(oldValues)){
  316. oldValues = [oldValues];
  317. }
  318. for(var i = 0; i < oldValues.length; i++){
  319. var value = oldValues[i];
  320. if(this.isItem(value)){
  321. this._removeReferenceFromMap(value, item, attribute);
  322. }
  323. }
  324. }
  325. }else{
  326. var newValueArray;
  327. if(dojo.isArray(newValueOrValues)){
  328. var newValues = newValueOrValues;
  329. // Unfortunately, it's not safe to just do this:
  330. // newValueArray = newValues;
  331. // Instead, we need to copy the array, which slice() does very nicely.
  332. // This is so that our internal data structure won't
  333. // get corrupted if the user mucks with the values array *after*
  334. // calling setValues().
  335. newValueArray = newValueOrValues.slice(0, newValueOrValues.length);
  336. }else{
  337. newValueArray = [newValueOrValues];
  338. }
  339. //We need to handle reference integrity if this is on.
  340. //In the case of set, we need to see if references were added or removed
  341. //and update the reference tracking map accordingly.
  342. if(this.referenceIntegrity){
  343. if(oldValueOrValues){
  344. var oldValues = oldValueOrValues;
  345. if(!dojo.isArray(oldValues)){
  346. oldValues = [oldValues];
  347. }
  348. //Use an associative map to determine what was added/removed from the list.
  349. //Should be O(n) performant. First look at all the old values and make a list of them
  350. //Then for any item not in the old list, we add it. If it was already present, we remove it.
  351. //Then we pass over the map and any references left it it need to be removed (IE, no match in
  352. //the new values list).
  353. var map = {};
  354. dojo.forEach(oldValues, function(possibleItem){
  355. if(this.isItem(possibleItem)){
  356. var id = this.getIdentity(possibleItem);
  357. map[id.toString()] = true;
  358. }
  359. }, this);
  360. dojo.forEach(newValueArray, function(possibleItem){
  361. if(this.isItem(possibleItem)){
  362. var id = this.getIdentity(possibleItem);
  363. if(map[id.toString()]){
  364. delete map[id.toString()];
  365. }else{
  366. this._addReferenceToMap(possibleItem, item, attribute);
  367. }
  368. }
  369. }, this);
  370. for(var rId in map){
  371. var removedItem;
  372. if(this._itemsByIdentity){
  373. removedItem = this._itemsByIdentity[rId];
  374. }else{
  375. removedItem = this._arrayOfAllItems[rId];
  376. }
  377. this._removeReferenceFromMap(removedItem, item, attribute);
  378. }
  379. }else{
  380. //Everything is new (no old values) so we have to just
  381. //insert all the references, if any.
  382. for(var i = 0; i < newValueArray.length; i++){
  383. var value = newValueArray[i];
  384. if(this.isItem(value)){
  385. this._addReferenceToMap(value, item, attribute);
  386. }
  387. }
  388. }
  389. }
  390. item[attribute] = newValueArray;
  391. success = true;
  392. }
  393. // Now we make the dojo.data.api.Notification call
  394. if(callOnSet){
  395. this.onSet(item, attribute, oldValueOrValues, newValueOrValues);
  396. }
  397. return success; // boolean
  398. },
  399. _addReferenceToMap: function(/*item*/ refItem, /*item*/ parentItem, /*string*/ attribute){
  400. // summary:
  401. // Method to add an reference map entry for an item and attribute.
  402. // description:
  403. // Method to add an reference map entry for an item and attribute. //
  404. // refItem:
  405. // The item that is referenced.
  406. // parentItem:
  407. // The item that holds the new reference to refItem.
  408. // attribute:
  409. // The attribute on parentItem that contains the new reference.
  410. var parentId = this.getIdentity(parentItem);
  411. var references = refItem[this._reverseRefMap];
  412. if(!references){
  413. references = refItem[this._reverseRefMap] = {};
  414. }
  415. var itemRef = references[parentId];
  416. if(!itemRef){
  417. itemRef = references[parentId] = {};
  418. }
  419. itemRef[attribute] = true;
  420. },
  421. _removeReferenceFromMap: function(/* item */ refItem, /* item */ parentItem, /*strin*/ attribute){
  422. // summary:
  423. // Method to remove an reference map entry for an item and attribute.
  424. // description:
  425. // Method to remove an reference map entry for an item and attribute. This will
  426. // also perform cleanup on the map such that if there are no more references at all to
  427. // the item, its reference object and entry are removed.
  428. //
  429. // refItem:
  430. // The item that is referenced.
  431. // parentItem:
  432. // The item holding a reference to refItem.
  433. // attribute:
  434. // The attribute on parentItem that contains the reference.
  435. var identity = this.getIdentity(parentItem);
  436. var references = refItem[this._reverseRefMap];
  437. var itemId;
  438. if(references){
  439. for(itemId in references){
  440. if(itemId == identity){
  441. delete references[itemId][attribute];
  442. if(this._isEmpty(references[itemId])){
  443. delete references[itemId];
  444. }
  445. }
  446. }
  447. if(this._isEmpty(references)){
  448. delete refItem[this._reverseRefMap];
  449. }
  450. }
  451. },
  452. _dumpReferenceMap: function(){
  453. // summary:
  454. // Function to dump the reverse reference map of all items in the store for debug purposes.
  455. // description:
  456. // Function to dump the reverse reference map of all items in the store for debug purposes.
  457. var i;
  458. for(i = 0; i < this._arrayOfAllItems.length; i++){
  459. var item = this._arrayOfAllItems[i];
  460. if(item && item[this._reverseRefMap]){
  461. console.log("Item: [" + this.getIdentity(item) + "] is referenced by: " + dojo.toJson(item[this._reverseRefMap]));
  462. }
  463. }
  464. },
  465. _getValueOrValues: function(/* item */ item, /* attribute-name-string */ attribute){
  466. var valueOrValues = undefined;
  467. if(this.hasAttribute(item, attribute)){
  468. var valueArray = this.getValues(item, attribute);
  469. if(valueArray.length == 1){
  470. valueOrValues = valueArray[0];
  471. }else{
  472. valueOrValues = valueArray;
  473. }
  474. }
  475. return valueOrValues;
  476. },
  477. _flatten: function(/* anything */ value){
  478. if(this.isItem(value)){
  479. var item = value;
  480. // Given an item, return an serializable object that provides a
  481. // reference to the item.
  482. // For example, given kermit:
  483. // var kermit = store.newItem({id:2, name:"Kermit"});
  484. // we want to return
  485. // {_reference:2}
  486. var identity = this.getIdentity(item);
  487. var referenceObject = {_reference: identity};
  488. return referenceObject;
  489. }else{
  490. if(typeof value === "object"){
  491. for(var type in this._datatypeMap){
  492. var typeMap = this._datatypeMap[type];
  493. if(dojo.isObject(typeMap) && !dojo.isFunction(typeMap)){
  494. if(value instanceof typeMap.type){
  495. if(!typeMap.serialize){
  496. throw new Error("ItemFileWriteStore: No serializer defined for type mapping: [" + type + "]");
  497. }
  498. return {_type: type, _value: typeMap.serialize(value)};
  499. }
  500. } else if(value instanceof typeMap){
  501. //SImple mapping, therefore, return as a toString serialization.
  502. return {_type: type, _value: value.toString()};
  503. }
  504. }
  505. }
  506. return value;
  507. }
  508. },
  509. _getNewFileContentString: function(){
  510. // summary:
  511. // Generate a string that can be saved to a file.
  512. // The result should look similar to:
  513. // http://trac.dojotoolkit.org/browser/dojo/trunk/tests/data/countries.json
  514. var serializableStructure = {};
  515. var identifierAttribute = this._getIdentifierAttribute();
  516. if(identifierAttribute !== Number){
  517. serializableStructure.identifier = identifierAttribute;
  518. }
  519. if(this._labelAttr){
  520. serializableStructure.label = this._labelAttr;
  521. }
  522. serializableStructure.items = [];
  523. for(var i = 0; i < this._arrayOfAllItems.length; ++i){
  524. var item = this._arrayOfAllItems[i];
  525. if(item !== null){
  526. var serializableItem = {};
  527. for(var key in item){
  528. if(key !== this._storeRefPropName && key !== this._itemNumPropName && key !== this._reverseRefMap && key !== this._rootItemPropName){
  529. var attribute = key;
  530. var valueArray = this.getValues(item, attribute);
  531. if(valueArray.length == 1){
  532. serializableItem[attribute] = this._flatten(valueArray[0]);
  533. }else{
  534. var serializableArray = [];
  535. for(var j = 0; j < valueArray.length; ++j){
  536. serializableArray.push(this._flatten(valueArray[j]));
  537. serializableItem[attribute] = serializableArray;
  538. }
  539. }
  540. }
  541. }
  542. serializableStructure.items.push(serializableItem);
  543. }
  544. }
  545. var prettyPrint = true;
  546. return dojo.toJson(serializableStructure, prettyPrint);
  547. },
  548. _isEmpty: function(something){
  549. // summary:
  550. // Function to determine if an array or object has no properties or values.
  551. // something:
  552. // The array or object to examine.
  553. var empty = true;
  554. if(dojo.isObject(something)){
  555. var i;
  556. for(i in something){
  557. empty = false;
  558. break;
  559. }
  560. }else if(dojo.isArray(something)){
  561. if(something.length > 0){
  562. empty = false;
  563. }
  564. }
  565. return empty; //boolean
  566. },
  567. save: function(/* object */ keywordArgs){
  568. // summary: See dojo.data.api.Write.save()
  569. this._assert(!this._saveInProgress);
  570. // this._saveInProgress is set to true, briefly, from when save is first called to when it completes
  571. this._saveInProgress = true;
  572. var self = this;
  573. var saveCompleteCallback = function(){
  574. self._pending = {
  575. _newItems:{},
  576. _modifiedItems:{},
  577. _deletedItems:{}
  578. };
  579. self._saveInProgress = false; // must come after this._pending is cleared, but before any callbacks
  580. if(keywordArgs && keywordArgs.onComplete){
  581. var scope = keywordArgs.scope || dojo.global;
  582. keywordArgs.onComplete.call(scope);
  583. }
  584. };
  585. var saveFailedCallback = function(){
  586. self._saveInProgress = false;
  587. if(keywordArgs && keywordArgs.onError){
  588. var scope = keywordArgs.scope || dojo.global;
  589. keywordArgs.onError.call(scope);
  590. }
  591. };
  592. if(this._saveEverything){
  593. var newFileContentString = this._getNewFileContentString();
  594. this._saveEverything(saveCompleteCallback, saveFailedCallback, newFileContentString);
  595. }
  596. if(this._saveCustom){
  597. this._saveCustom(saveCompleteCallback, saveFailedCallback);
  598. }
  599. if(!this._saveEverything && !this._saveCustom){
  600. // Looks like there is no user-defined save-handler function.
  601. // That's fine, it just means the datastore is acting as a "mock-write"
  602. // store -- changes get saved in memory but don't get saved to disk.
  603. saveCompleteCallback();
  604. }
  605. },
  606. revert: function(){
  607. // summary: See dojo.data.api.Write.revert()
  608. this._assert(!this._saveInProgress);
  609. var identity;
  610. for(identity in this._pending._modifiedItems){
  611. // find the original item and the modified item that replaced it
  612. var copyOfItemState = this._pending._modifiedItems[identity];
  613. var modifiedItem = null;
  614. if(this._itemsByIdentity){
  615. modifiedItem = this._itemsByIdentity[identity];
  616. }else{
  617. modifiedItem = this._arrayOfAllItems[identity];
  618. }
  619. // Restore the original item into a full-fledged item again, we want to try to
  620. // keep the same object instance as if we don't it, causes bugs like #9022.
  621. copyOfItemState[this._storeRefPropName] = this;
  622. for(key in modifiedItem){
  623. delete modifiedItem[key];
  624. }
  625. dojo.mixin(modifiedItem, copyOfItemState);
  626. }
  627. var deletedItem;
  628. for(identity in this._pending._deletedItems){
  629. deletedItem = this._pending._deletedItems[identity];
  630. deletedItem[this._storeRefPropName] = this;
  631. var index = deletedItem[this._itemNumPropName];
  632. //Restore the reverse refererence map, if any.
  633. if(deletedItem["backup_" + this._reverseRefMap]){
  634. deletedItem[this._reverseRefMap] = deletedItem["backup_" + this._reverseRefMap];
  635. delete deletedItem["backup_" + this._reverseRefMap];
  636. }
  637. this._arrayOfAllItems[index] = deletedItem;
  638. if(this._itemsByIdentity){
  639. this._itemsByIdentity[identity] = deletedItem;
  640. }
  641. if(deletedItem[this._rootItemPropName]){
  642. this._arrayOfTopLevelItems.push(deletedItem);
  643. }
  644. }
  645. //We have to pass through it again and restore the reference maps after all the
  646. //undeletes have occurred.
  647. for(identity in this._pending._deletedItems){
  648. deletedItem = this._pending._deletedItems[identity];
  649. if(deletedItem["backupRefs_" + this._reverseRefMap]){
  650. dojo.forEach(deletedItem["backupRefs_" + this._reverseRefMap], function(reference){
  651. var refItem;
  652. if(this._itemsByIdentity){
  653. refItem = this._itemsByIdentity[reference.id];
  654. }else{
  655. refItem = this._arrayOfAllItems[reference.id];
  656. }
  657. this._addReferenceToMap(refItem, deletedItem, reference.attr);
  658. }, this);
  659. delete deletedItem["backupRefs_" + this._reverseRefMap];
  660. }
  661. }
  662. for(identity in this._pending._newItems){
  663. var newItem = this._pending._newItems[identity];
  664. newItem[this._storeRefPropName] = null;
  665. // null out the new item, but don't change the array index so
  666. // so we can keep using _arrayOfAllItems.length.
  667. this._arrayOfAllItems[newItem[this._itemNumPropName]] = null;
  668. if(newItem[this._rootItemPropName]){
  669. this._removeArrayElement(this._arrayOfTopLevelItems, newItem);
  670. }
  671. if(this._itemsByIdentity){
  672. delete this._itemsByIdentity[identity];
  673. }
  674. }
  675. this._pending = {
  676. _newItems:{},
  677. _modifiedItems:{},
  678. _deletedItems:{}
  679. };
  680. return true; // boolean
  681. },
  682. isDirty: function(/* item? */ item){
  683. // summary: See dojo.data.api.Write.isDirty()
  684. if(item){
  685. // return true if the item is dirty
  686. var identity = this.getIdentity(item);
  687. return new Boolean(this._pending._newItems[identity] ||
  688. this._pending._modifiedItems[identity] ||
  689. this._pending._deletedItems[identity]).valueOf(); // boolean
  690. }else{
  691. // return true if the store is dirty -- which means return true
  692. // if there are any new items, dirty items, or modified items
  693. if(!this._isEmpty(this._pending._newItems) ||
  694. !this._isEmpty(this._pending._modifiedItems) ||
  695. !this._isEmpty(this._pending._deletedItems)){
  696. return true;
  697. }
  698. return false; // boolean
  699. }
  700. },
  701. /* dojo.data.api.Notification */
  702. onSet: function(/* item */ item,
  703. /*attribute-name-string*/ attribute,
  704. /*object | array*/ oldValue,
  705. /*object | array*/ newValue){
  706. // summary: See dojo.data.api.Notification.onSet()
  707. // No need to do anything. This method is here just so that the
  708. // client code can connect observers to it.
  709. },
  710. onNew: function(/* item */ newItem, /*object?*/ parentInfo){
  711. // summary: See dojo.data.api.Notification.onNew()
  712. // No need to do anything. This method is here just so that the
  713. // client code can connect observers to it.
  714. },
  715. onDelete: function(/* item */ deletedItem){
  716. // summary: See dojo.data.api.Notification.onDelete()
  717. // No need to do anything. This method is here just so that the
  718. // client code can connect observers to it.
  719. },
  720. close: function(/* object? */ request){
  721. // summary:
  722. // Over-ride of base close function of ItemFileReadStore to add in check for store state.
  723. // description:
  724. // Over-ride of base close function of ItemFileReadStore to add in check for store state.
  725. // If the store is still dirty (unsaved changes), then an error will be thrown instead of
  726. // clearing the internal state for reload from the url.
  727. //Clear if not dirty ... or throw an error
  728. if(this.clearOnClose){
  729. if(!this.isDirty()){
  730. this.inherited(arguments);
  731. }else{
  732. //Only throw an error if the store was dirty and we were loading from a url (cannot reload from url until state is saved).
  733. throw new Error("dojox.data.AndOrWriteStore: There are unsaved changes present in the store. Please save or revert the changes before invoking close.");
  734. }
  735. }
  736. }
  737. });
  738. }