Store.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  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. define([], function() {
  7. // module:
  8. // dojo/store/api/Store
  9. // summary:
  10. // The module defines the Dojo object store interface.
  11. dojo.declare("dojo.store.api.Store", null, {
  12. // summary:
  13. // This is an abstract API that data provider implementations conform to.
  14. // This file defines methods signatures and intentionally leaves all the
  15. // methods unimplemented. For more information on the dojo.store APIs,
  16. // please visit: http://dojotoolkit.org/reference-guide/dojo/store.html
  17. // Every method and property is optional, and is only needed if the functionality
  18. // it provides is required.
  19. // Every method may return a promise for the specified return value if the
  20. // execution of the operation is asynchronous (except
  21. // for query() which already defines an async return value).
  22. // idProperty: String
  23. // If the store has a single primary key, this tndicates the property to use as the
  24. // identity property. The values of this property should be unique.
  25. idProperty: "id",
  26. // queryEngine: Function
  27. // If the store can be queried locally (on the client side in JS), this defines
  28. // the query engine to use for querying the data store.
  29. // This takes a query and query options and returns a function that can execute
  30. // the provided query on a JavaScript array. The queryEngine may be replace to
  31. // provide more sophisticated querying capabilities. For example:
  32. // | var query = store.queryEngine({foo:"bar"}, {count:10});
  33. // | query(someArray) -> filtered array
  34. // The returned query function may have a "matches" property that can be
  35. // used to determine if an object matches the query. For example:
  36. // | query.matches({id:"some-object", foo:"bar"}) -> true
  37. // | query.matches({id:"some-object", foo:"something else"}) -> false
  38. queryEngine: null,
  39. get: function(id){
  40. // summary:
  41. // Retrieves an object by its identity
  42. // id: Number
  43. // The identity to use to lookup the object
  44. // returns: Object
  45. // The object in the store that matches the given id.
  46. },
  47. getIdentity: function(object){
  48. // summary:
  49. // Returns an object's identity
  50. // object: Object
  51. // The object to get the identity from
  52. // returns: String|Number
  53. },
  54. put: function(object, directives){
  55. // summary:
  56. // Stores an object
  57. // object: Object
  58. // The object to store.
  59. // directives: dojo.store.api.Store.PutDirectives?
  60. // Additional directives for storing objects.
  61. // returns: Number|String
  62. },
  63. add: function(object, directives){
  64. // summary:
  65. // Creates an object, throws an error if the object already exists
  66. // object: Object
  67. // The object to store.
  68. // directives: dojo.store.api.Store.PutDirectives?
  69. // Additional directives for creating objects.
  70. // returns: Number|String
  71. },
  72. remove: function(id){
  73. // summary:
  74. // Deletes an object by its identity
  75. // id: Number
  76. // The identity to use to delete the object
  77. delete this.index[id];
  78. var data = this.data,
  79. idProperty = this.idProperty;
  80. for(var i = 0, l = data.length; i < l; i++){
  81. if(data[i][idProperty] == id){
  82. data.splice(i, 1);
  83. return;
  84. }
  85. }
  86. },
  87. query: function(query, options){
  88. // summary:
  89. // Queries the store for objects. This does not alter the store, but returns a
  90. // set of data from the store.
  91. // query: String|Object|Function
  92. // The query to use for retrieving objects from the store.
  93. // options: dojo.store.api.Store.QueryOptions
  94. // The optional arguments to apply to the resultset.
  95. // returns: dojo.store.api.Store.QueryResults
  96. // The results of the query, extended with iterative methods.
  97. //
  98. // example:
  99. // Given the following store:
  100. //
  101. // ...find all items where "prime" is true:
  102. //
  103. // | store.query({ prime: true }).forEach(function(object){
  104. // | // handle each object
  105. // | });
  106. },
  107. transaction: function(){
  108. // summary:
  109. // Starts a new transaction.
  110. // Note that a store user might not call transaction() prior to using put,
  111. // delete, etc. in which case these operations effectively could be thought of
  112. // as "auto-commit" style actions.
  113. // returns: dojo.store.api.Store.Transaction
  114. // This represents the new current transaction.
  115. },
  116. getChildren: function(parent, options){
  117. // summary:
  118. // Retrieves the children of an object.
  119. // parent: Object
  120. // The object to find the children of.
  121. // options: dojo.store.api.Store.QueryOptions?
  122. // Additional options to apply to the retrieval of the children.
  123. // returns: dojo.store.api.Store.QueryResults
  124. // A result set of the children of the parent object.
  125. },
  126. getMetadata: function(object){
  127. // summary:
  128. // Returns any metadata about the object. This may include attribution,
  129. // cache directives, history, or version information.
  130. // object: Object
  131. // The object to return metadata for.
  132. // returns: Object
  133. // An object containing metadata.
  134. }
  135. });
  136. dojo.store.api.Store.PutDirectives = function(id, before, parent, overwrite){
  137. // summary:
  138. // Directives passed to put() and add() handlers for guiding the update and
  139. // creation of stored objects.
  140. // id: String|Number?
  141. // Indicates the identity of the object if a new object is created
  142. // before: Object?
  143. // If the collection of objects in the store has a natural ordering,
  144. // this indicates that the created or updated object should be placed before the
  145. // object specified by the value of this property. A value of null indicates that the
  146. // object should be last.
  147. // parent: Object?,
  148. // If the store is hierarchical (with single parenting) this property indicates the
  149. // new parent of the created or updated object.
  150. // overwrite: Boolean?
  151. // If this is provided as a boolean it indicates that the object should or should not
  152. // overwrite an existing object. A value of true indicates that a new object
  153. // should not be created, the operation should update an existing object. A
  154. // value of false indicates that an existing object should not be updated, a new
  155. // object should be created (which is the same as an add() operation). When
  156. // this property is not provided, either an update or creation is acceptable.
  157. this.id = id;
  158. this.before = before;
  159. this.parent = parent;
  160. this.overwrite = overwrite;
  161. };
  162. dojo.store.api.Store.SortInformation = function(attribute, descending){
  163. // summary:
  164. // An object describing what attribute to sort on, and the direction of the sort.
  165. // attribute: String
  166. // The name of the attribute to sort on.
  167. // descending: Boolean
  168. // The direction of the sort. Default is false.
  169. this.attribute = attribute;
  170. this.descending = descending;
  171. };
  172. dojo.store.api.Store.QueryOptions = function(sort, start, count){
  173. // summary:
  174. // Optional object with additional parameters for query results.
  175. // sort: dojo.store.api.Store.SortInformation[]?
  176. // A list of attributes to sort on, as well as direction
  177. // For example:
  178. // | [{attribute:"price, descending: true}].
  179. // If the sort parameter is omitted, then the natural order of the store may be
  180. // applied if there is a natural order.
  181. // start: Number?
  182. // The first result to begin iteration on
  183. // count: Number?
  184. // The number of how many results should be returned.
  185. this.sort = sort;
  186. this.start = start;
  187. this.count = count;
  188. };
  189. dojo.declare("dojo.store.api.Store.QueryResults", null, {
  190. // summary:
  191. // This is an object returned from query() calls that provides access to the results
  192. // of a query. Queries may be executed asynchronously.
  193. forEach: function(callback, thisObject){
  194. // summary:
  195. // Iterates over the query results, based on
  196. // https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Objects/Array/forEach.
  197. // Note that this may executed asynchronously. The callback may be called
  198. // after this function returns.
  199. // callback:
  200. // Function that is called for each object in the query results
  201. // thisObject:
  202. // The object to use as |this| in the callback.
  203. },
  204. filter: function(callback, thisObject){
  205. // summary:
  206. // Filters the query results, based on
  207. // https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Objects/Array/filter.
  208. // Note that this may executed asynchronously. The callback may be called
  209. // after this function returns.
  210. // callback:
  211. // Function that is called for each object in the query results
  212. // thisObject:
  213. // The object to use as |this| in the callback.
  214. // returns: dojo.store.api.Store.QueryResults
  215. },
  216. map: function(callback, thisObject){
  217. // summary:
  218. // Maps the query results, based on
  219. // https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Objects/Array/map.
  220. // Note that this may executed asynchronously. The callback may be called
  221. // after this function returns.
  222. // callback:
  223. // Function that is called for each object in the query results
  224. // thisObject:
  225. // The object to use as |this| in the callback.
  226. // returns: dojo.store.api.Store.QueryResults
  227. },
  228. then: function(callback, errorHandler){
  229. // summary:
  230. // This registers a callback for when the query is complete, if the query is asynchronous.
  231. // This is an optional method, and may not be present for synchronous queries.
  232. // callback:
  233. // This is called when the query is completed successfully, and is passed a single argument
  234. // that is an array representing the query results.
  235. // errorHandler:
  236. // This is called if the query failed, and is passed a single argument that is the error
  237. // for the failure.
  238. },
  239. observe: function(listener, includeAllUpdates){
  240. // summary:
  241. // This registers a callback for notification of when data is modified in the query results.
  242. // This is an optional method, and is usually provided by dojo.store.Observable.
  243. // listener: Function
  244. // The listener function is called when objects in the query results are modified
  245. // to affect the query result. The listener function is called with the following
  246. // arguments:
  247. // | listener(object, removedFrom, insertedInto);
  248. // * The object parameter indicates the object that was create, modified, or deleted.
  249. // * The removedFrom parameter indicates the index in the result array where
  250. // the object used to be. If the value is -1, then the object is an addition to
  251. // this result set (due to a new object being created, or changed such that it
  252. // is a part of the result set).
  253. // * The insertedInto parameter indicates the index in the result array where
  254. // the object should be now. If the value is -1, then the object is a removal
  255. // from this result set (due to an object being deleted, or changed such that it
  256. // is not a part of the result set).
  257. // includeAllUpdates:
  258. // This indicates whether or not to include object updates that do not affect
  259. // the inclusion or order of the object in the query results. By default this is false,
  260. // which means that if any object is updated in such a way that it remains
  261. // in the result set and it's position in result sets is not affected, then the listener
  262. // will not be fired.
  263. },
  264. // total: Number|Promise?
  265. // This property should be included in if the query options included the "count"
  266. // property limiting the result set. This property indicates the total number of objects
  267. // matching the query (as if "start" and "count" weren't present). This may be
  268. // a promise if the query is asynchronous.
  269. total: 0
  270. });
  271. dojo.declare("dojo.store.api.Store.Transaction", null, {
  272. // summary:
  273. // This is an object returned from transaction() calls that represents the current
  274. // transaction.
  275. commit: function(){
  276. // summary:
  277. // Commits the transaction. This may throw an error if it fails. Of if the operation
  278. // is asynchronous, it may return a promise that represents the eventual success
  279. // or failure of the commit.
  280. },
  281. abort: function(callback, thisObject){
  282. // summary:
  283. // Aborts the transaction. This may throw an error if it fails. Of if the operation
  284. // is asynchronous, it may return a promise that represents the eventual success
  285. // or failure of the abort.
  286. }
  287. });
  288. });