Memory.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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["dojo.store.Memory"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
  7. dojo._hasResource["dojo.store.Memory"] = true;
  8. dojo.provide("dojo.store.Memory");
  9. dojo.require("dojo.store.util.QueryResults");
  10. dojo.require("dojo.store.util.SimpleQueryEngine");
  11. dojo.declare("dojo.store.Memory", null, {
  12. // summary:
  13. // This is a basic in-memory object store. It implements dojo.store.api.Store.
  14. constructor: function(/*dojo.store.Memory*/ options){
  15. // summary:
  16. // Creates a memory object store.
  17. // options:
  18. // This provides any configuration information that will be mixed into the store.
  19. // This should generally include the data property to provide the starting set of data.
  20. this.index = {};
  21. dojo.mixin(this, options);
  22. this.setData(this.data || []);
  23. },
  24. // data: Array
  25. // The array of all the objects in the memory store
  26. data:null,
  27. // idProperty: String
  28. // Indicates the property to use as the identity property. The values of this
  29. // property should be unique.
  30. idProperty: "id",
  31. // index: Object
  32. // An index of data by id
  33. index:null,
  34. // queryEngine: Function
  35. // Defines the query engine to use for querying the data store
  36. queryEngine: dojo.store.util.SimpleQueryEngine,
  37. get: function(id){
  38. // summary:
  39. // Retrieves an object by its identity
  40. // id: Number
  41. // The identity to use to lookup the object
  42. // returns: Object
  43. // The object in the store that matches the given id.
  44. return this.index[id];
  45. },
  46. getIdentity: function(object){
  47. // summary:
  48. // Returns an object's identity
  49. // object: Object
  50. // The object to get the identity from
  51. // returns: Number
  52. return object[this.idProperty];
  53. },
  54. put: function(object, options){
  55. // summary:
  56. // Stores an object
  57. // object: Object
  58. // The object to store.
  59. // options: dojo.store.api.Store.PutDirectives??
  60. // Additional metadata for storing the data. Includes an "id"
  61. // property if a specific id is to be used.
  62. // returns: Number
  63. var id = options && options.id || object[this.idProperty] || Math.random();
  64. this.index[id] = object;
  65. var data = this.data,
  66. idProperty = this.idProperty;
  67. for(var i = 0, l = data.length; i < l; i++){
  68. if(data[i][idProperty] == id){
  69. data[i] = object;
  70. return id;
  71. }
  72. }
  73. this.data.push(object);
  74. return id;
  75. },
  76. add: function(object, options){
  77. // summary:
  78. // Creates an object, throws an error if the object already exists
  79. // object: Object
  80. // The object to store.
  81. // options: dojo.store.api.Store.PutDirectives??
  82. // Additional metadata for storing the data. Includes an "id"
  83. // property if a specific id is to be used.
  84. // returns: Number
  85. if(this.index[options && options.id || object[this.idProperty]]){
  86. throw new Error("Object already exists");
  87. }
  88. return this.put(object, options);
  89. },
  90. remove: function(id){
  91. // summary:
  92. // Deletes an object by its identity
  93. // id: Number
  94. // The identity to use to delete the object
  95. delete this.index[id];
  96. var data = this.data,
  97. idProperty = this.idProperty;
  98. for(var i = 0, l = data.length; i < l; i++){
  99. if(data[i][idProperty] == id){
  100. data.splice(i, 1);
  101. return;
  102. }
  103. }
  104. },
  105. query: function(query, options){
  106. // summary:
  107. // Queries the store for objects.
  108. // query: Object
  109. // The query to use for retrieving objects from the store.
  110. // options: dojo.store.api.Store.QueryOptions?
  111. // The optional arguments to apply to the resultset.
  112. // returns: dojo.store.api.Store.QueryResults
  113. // The results of the query, extended with iterative methods.
  114. //
  115. // example:
  116. // Given the following store:
  117. //
  118. // | var store = new dojo.store.Memory({
  119. // | data: [
  120. // | {id: 1, name: "one", prime: false },
  121. // | {id: 2, name: "two", even: true, prime: true},
  122. // | {id: 3, name: "three", prime: true},
  123. // | {id: 4, name: "four", even: true, prime: false},
  124. // | {id: 5, name: "five", prime: true}
  125. // | ]
  126. // | });
  127. //
  128. // ...find all items where "prime" is true:
  129. //
  130. // | var results = store.query({ prime: true });
  131. //
  132. // ...or find all items where "even" is true:
  133. //
  134. // | var results = store.query({ even: true });
  135. return dojo.store.util.QueryResults(this.queryEngine(query, options)(this.data));
  136. },
  137. setData: function(data){
  138. // summary:
  139. // Sets the given data as the source for this store, and indexes it
  140. // data: Object[]
  141. // An array of objects to use as the source of data.
  142. if(data.items){
  143. // just for convenience with the data format IFRS expects
  144. this.idProperty = data.identifier;
  145. data = this.data = data.items;
  146. }else{
  147. this.data = data;
  148. }
  149. for(var i = 0, l = data.length; i < l; i++){
  150. var object = data[i];
  151. this.index[object[this.idProperty]] = object;
  152. }
  153. }
  154. });
  155. }