DataStore.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. define("dojo/store/DataStore", ["../_base/lang", "../_base/declare", "../_base/Deferred", "../_base/array", "./util/QueryResults"
  2. ], function(lang,declare,Deferred,array,QueryResults) {
  3. // module:
  4. // dojo/store/DataStore
  5. // summary:
  6. // TODOC
  7. return declare("dojo.store.DataStore", null, {
  8. target: "",
  9. constructor: function(options){
  10. // summary:
  11. // This is an adapter for using Dojo Data stores with an object store consumer.
  12. // You can provide a Dojo data store and use this adapter to interact with it through
  13. // the Dojo object store API
  14. // options: Object?
  15. // This provides any configuration information that will be mixed into the store,
  16. // including a reference to the Dojo data store under the property "store".
  17. lang.mixin(this, options);
  18. if(!"idProperty" in options){
  19. var idAttribute;
  20. try{
  21. idAttribute = this.store.getIdentityAttributes();
  22. }catch(e){
  23. // some store are not requiring an item instance to give us the ID attributes
  24. // but some other do and throw errors in that case.
  25. }
  26. // if no idAttribute we have implicit id
  27. this.idProperty = (!idAttribute || !idAttributes[0]) || this.idProperty;
  28. }
  29. var features = this.store.getFeatures();
  30. // check the feature set and null out any methods that shouldn't be available
  31. if(!features["dojo.data.api.Read"]){
  32. this.get = null;
  33. }
  34. if(!features["dojo.data.api.Identity"]){
  35. this.getIdentity = null;
  36. }
  37. if(!features["dojo.data.api.Write"]){
  38. this.put = this.add = null;
  39. }
  40. },
  41. // idProperty: String
  42. // The object property to use to store the identity of the store items.
  43. idProperty: "id",
  44. // store:
  45. // The object store to convert to a data store
  46. store: null,
  47. _objectConverter: function(callback){
  48. var store = this.store;
  49. var idProperty = this.idProperty;
  50. return function(item){
  51. var object = {};
  52. var attributes = store.getAttributes(item);
  53. for(var i = 0; i < attributes.length; i++){
  54. object[attributes[i]] = store.getValue(item, attributes[i]);
  55. }
  56. if(!(idProperty in object)){
  57. object[idProperty] = store.getIdentity(item);
  58. }
  59. return callback(object);
  60. };
  61. },
  62. get: function(id, options){
  63. // summary:
  64. // Retrieves an object by it's identity. This will trigger a fetchItemByIdentity
  65. // id: Object?
  66. // The identity to use to lookup the object
  67. var returnedObject, returnedError;
  68. var deferred = new Deferred();
  69. this.store.fetchItemByIdentity({
  70. identity: id,
  71. onItem: this._objectConverter(function(object){
  72. deferred.resolve(returnedObject = object);
  73. }),
  74. onError: function(error){
  75. deferred.reject(returnedError = error);
  76. }
  77. });
  78. if(returnedObject){
  79. // if it was returned synchronously
  80. return returnedObject;
  81. }
  82. if(returnedError){
  83. throw returnedError;
  84. }
  85. return deferred.promise;
  86. },
  87. put: function(object, options){
  88. // summary:
  89. // Stores an object by its identity.
  90. // object: Object
  91. // The object to store.
  92. // options: Object?
  93. // Additional metadata for storing the data. Includes a reference to an id
  94. // that the object may be stored with (i.e. { id: "foo" }).
  95. var id = options && typeof options.id != "undefined" || this.getIdentity(object);
  96. var store = this.store;
  97. var idProperty = this.idProperty;
  98. if(typeof id == "undefined"){
  99. store.newItem(object);
  100. }else{
  101. store.fetchItemByIdentity({
  102. identity: id,
  103. onItem: function(item){
  104. if(item){
  105. for(var i in object){
  106. if(i != idProperty && // don't copy id properties since they are immutable and should be omitted for implicit ids
  107. store.getValue(item, i) != object[i]){
  108. store.setValue(item, i, object[i]);
  109. }
  110. }
  111. }else{
  112. store.newItem(object);
  113. }
  114. }
  115. });
  116. }
  117. },
  118. remove: function(id){
  119. // summary:
  120. // Deletes an object by its identity.
  121. // id: Object
  122. // The identity to use to delete the object
  123. var store = this.store;
  124. this.store.fetchItemByIdentity({
  125. identity: id,
  126. onItem: function(item){
  127. store.deleteItem(item);
  128. }
  129. });
  130. },
  131. query: function(query, options){
  132. // summary:
  133. // Queries the store for objects.
  134. // query: Object
  135. // The query to use for retrieving objects from the store
  136. // options: Object?
  137. // Optional options object as used by the underlying dojo.data Store.
  138. // returns: dojo.store.util.QueryResults
  139. // A query results object that can be used to iterate over results.
  140. var fetchHandle;
  141. var deferred = new Deferred(function(){ fetchHandle.abort && fetchHandle.abort(); });
  142. deferred.total = new Deferred();
  143. var converter = this._objectConverter(function(object){return object;});
  144. fetchHandle = this.store.fetch(lang.mixin({
  145. query: query,
  146. onBegin: function(count){
  147. deferred.total.resolve(count);
  148. },
  149. onComplete: function(results){
  150. deferred.resolve(array.map(results, converter));
  151. },
  152. onError: function(error){
  153. deferred.reject(error);
  154. }
  155. }, options));
  156. return QueryResults(deferred);
  157. },
  158. getIdentity: function(object){
  159. // summary:
  160. // Fetch the identity for the given object.
  161. // object: Object
  162. // The data object to get the identity from.
  163. // returns: Number
  164. // The id of the given object.
  165. return object[this.idProperty];
  166. }
  167. });
  168. });