Store.js 12 KB

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