DataStore.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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.DataStore"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
  7. dojo._hasResource["dojo.store.DataStore"] = true;
  8. dojo.provide("dojo.store.DataStore");
  9. dojo.require("dojo.store.util.QueryResults");
  10. dojo.declare("dojo.store.DataStore", null, {
  11. target: "",
  12. constructor: function(options){
  13. // summary:
  14. // This is an adapter for using Dojo Data stores with an object store consumer.
  15. // You can provide a Dojo data store and use this adapter to interact with it through
  16. // the Dojo object store API
  17. // options: Object?
  18. // This provides any configuration information that will be mixed into the store,
  19. // including a reference to the Dojo data store under the property "store".
  20. dojo.mixin(this, options);
  21. },
  22. _objectConverter: function(callback){
  23. var store = this.store;
  24. return function(item){
  25. var object = {};
  26. var attributes = store.getAttributes(item);
  27. for(var i = 0; i < attributes.length; i++){
  28. object[attributes[i]] = store.getValue(item, attributes[i]);
  29. }
  30. return callback(object);
  31. };
  32. },
  33. get: function(id, options){
  34. // summary:
  35. // Retrieves an object by it's identity. This will trigger a fetchItemByIdentity
  36. // id: Object?
  37. // The identity to use to lookup the object
  38. var returnedObject, returnedError;
  39. var deferred = new dojo.Deferred();
  40. this.store.fetchItemByIdentity({
  41. identity: id,
  42. onItem: this._objectConverter(function(object){
  43. deferred.resolve(returnedObject = object);
  44. }),
  45. onError: function(error){
  46. deferred.reject(returnedError = error);
  47. }
  48. });
  49. if(returnedObject){
  50. // if it was returned synchronously
  51. return returnedObject;
  52. }
  53. if(returnedError){
  54. throw returnedError;
  55. }
  56. return deferred.promise;
  57. },
  58. put: function(object, options){
  59. // summary:
  60. // Stores an object by its identity.
  61. // object: Object
  62. // The object to store.
  63. // options: Object?
  64. // Additional metadata for storing the data. Includes a reference to an id
  65. // that the object may be stored with (i.e. { id: "foo" }).
  66. var id = options && typeof options.id != "undefined" || this.getIdentity(object);
  67. var store = this.store;
  68. if(typeof id == "undefined"){
  69. store.newItem(object);
  70. }else{
  71. store.fetchItemByIdentity({
  72. identity: id,
  73. onItem: function(item){
  74. if(item){
  75. for(var i in object){
  76. if(store.getValue(item, i) != object[i]){
  77. store.setValue(item, i, object[i]);
  78. }
  79. }
  80. }else{
  81. store.newItem(object);
  82. }
  83. }
  84. });
  85. }
  86. },
  87. remove: function(id){
  88. // summary:
  89. // Deletes an object by its identity.
  90. // id: Object
  91. // The identity to use to delete the object
  92. var store = this.store;
  93. this.store.fetchItemByIdentity({
  94. identity: id,
  95. onItem: function(item){
  96. store.deleteItem(item);
  97. }
  98. });
  99. },
  100. query: function(query, options){
  101. // summary:
  102. // Queries the store for objects.
  103. // query: Object
  104. // The query to use for retrieving objects from the store
  105. // options: Object?
  106. // Optional options object as used by the underlying dojo.data Store.
  107. // returns: dojo.store.util.QueryResults
  108. // A query results object that can be used to iterate over results.
  109. var returnedObject, returnedError;
  110. var deferred = new dojo.Deferred();
  111. deferred.total = new dojo.Deferred();
  112. var converter = this._objectConverter(function(object){return object;});
  113. this.store.fetch(dojo.mixin({
  114. query: query,
  115. onBegin: function(count){
  116. deferred.total.resolve(count);
  117. },
  118. onComplete: function(results){
  119. deferred.resolve(dojo.map(results, converter));
  120. },
  121. onError: function(error){
  122. deferred.reject(error);
  123. }
  124. }, options));
  125. return dojo.store.util.QueryResults(deferred);
  126. },
  127. getIdentity: function(object){
  128. // summary:
  129. // Fetch the identity for the given object.
  130. // object: Object
  131. // The data object to get the identity from.
  132. // returns: Number
  133. // The id of the given object.
  134. return object[this.idProperty || this.store.getIdentityAttributes()[0]];
  135. }
  136. });
  137. }