Memory.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. define("dojo/store/Memory", ["../_base/declare", "./util/QueryResults", "./util/SimpleQueryEngine"], function(declare, QueryResults, SimpleQueryEngine) {
  2. // module:
  3. // dojo/store/Memory
  4. // summary:
  5. // The module defines an in-memory object store.
  6. return declare("dojo.store.Memory", null, {
  7. // summary:
  8. // This is a basic in-memory object store. It implements dojo.store.api.Store.
  9. constructor: function(/*dojo.store.Memory*/ options){
  10. // summary:
  11. // Creates a memory object store.
  12. // options:
  13. // This provides any configuration information that will be mixed into the store.
  14. // This should generally include the data property to provide the starting set of data.
  15. for(var i in options){
  16. this[i] = options[i];
  17. }
  18. this.setData(this.data || []);
  19. },
  20. // data: Array
  21. // The array of all the objects in the memory store
  22. data:null,
  23. // idProperty: String
  24. // Indicates the property to use as the identity property. The values of this
  25. // property should be unique.
  26. idProperty: "id",
  27. // index: Object
  28. // An index of data indices into the data array by id
  29. index:null,
  30. // queryEngine: Function
  31. // Defines the query engine to use for querying the data store
  32. queryEngine: SimpleQueryEngine,
  33. get: function(id){
  34. // summary:
  35. // Retrieves an object by its identity
  36. // id: Number
  37. // The identity to use to lookup the object
  38. // returns: Object
  39. // The object in the store that matches the given id.
  40. return this.data[this.index[id]];
  41. },
  42. getIdentity: function(object){
  43. // summary:
  44. // Returns an object's identity
  45. // object: Object
  46. // The object to get the identity from
  47. // returns: Number
  48. return object[this.idProperty];
  49. },
  50. put: function(object, options){
  51. // summary:
  52. // Stores an object
  53. // object: Object
  54. // The object to store.
  55. // options: dojo.store.api.Store.PutDirectives??
  56. // Additional metadata for storing the data. Includes an "id"
  57. // property if a specific id is to be used.
  58. // returns: Number
  59. var data = this.data,
  60. index = this.index,
  61. idProperty = this.idProperty;
  62. var id = (options && "id" in options) ? options.id : idProperty in object ? object[idProperty] : Math.random();
  63. if(id in index){
  64. // object exists
  65. if(options && options.overwrite === false){
  66. throw new Error("Object already exists");
  67. }
  68. // replace the entry in data
  69. data[index[id]] = object;
  70. }else{
  71. // add the new object
  72. index[id] = data.push(object) - 1;
  73. }
  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. (options = options || {}).overwrite = false;
  86. // call put with overwrite being false
  87. return this.put(object, options);
  88. },
  89. remove: function(id){
  90. // summary:
  91. // Deletes an object by its identity
  92. // id: Number
  93. // The identity to use to delete the object
  94. // returns: Boolean
  95. // Returns true if an object was removed, falsy (undefined) if no object matched the id
  96. var index = this.index;
  97. var data = this.data;
  98. if(id in index){
  99. data.splice(index[id], 1);
  100. // now we have to reindex
  101. this.setData(data);
  102. return true;
  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 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 || this.idProperty;
  145. data = this.data = data.items;
  146. }else{
  147. this.data = data;
  148. }
  149. this.index = {};
  150. for(var i = 0, l = data.length; i < l; i++){
  151. this.index[data[i][this.idProperty]] = i;
  152. }
  153. }
  154. });
  155. });