2 Copyright (c) 2004-2011, 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
8 if(!dojo._hasResource["dojo.store.DataStore"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
9 dojo._hasResource["dojo.store.DataStore"] = true;
10 dojo.provide("dojo.store.DataStore");
11 dojo.require("dojo.store.util.QueryResults");
14 dojo.declare("dojo.store.DataStore", null, {
16 constructor: function(options){
18 // This is an adapter for using Dojo Data stores with an object store consumer.
19 // You can provide a Dojo data store and use this adapter to interact with it through
20 // the Dojo object store API
22 // This provides any configuration information that will be mixed into the store,
23 // including a reference to the Dojo data store under the property "store".
24 dojo.mixin(this, options);
26 _objectConverter: function(callback){
27 var store = this.store;
28 return function(item){
30 var attributes = store.getAttributes(item);
31 for(var i = 0; i < attributes.length; i++){
32 object[attributes[i]] = store.getValue(item, attributes[i]);
34 return callback(object);
37 get: function(id, options){
39 // Retrieves an object by it's identity. This will trigger a fetchItemByIdentity
41 // The identity to use to lookup the object
42 var returnedObject, returnedError;
43 var deferred = new dojo.Deferred();
44 this.store.fetchItemByIdentity({
46 onItem: this._objectConverter(function(object){
47 deferred.resolve(returnedObject = object);
49 onError: function(error){
50 deferred.reject(returnedError = error);
54 // if it was returned synchronously
55 return returnedObject;
60 return deferred.promise;
62 put: function(object, options){
64 // Stores an object by its identity.
66 // The object to store.
68 // Additional metadata for storing the data. Includes a reference to an id
69 // that the object may be stored with (i.e. { id: "foo" }).
70 var id = options && typeof options.id != "undefined" || this.getIdentity(object);
71 var store = this.store;
72 if(typeof id == "undefined"){
73 store.newItem(object);
75 store.fetchItemByIdentity({
77 onItem: function(item){
80 if(store.getValue(item, i) != object[i]){
81 store.setValue(item, i, object[i]);
85 store.newItem(object);
93 // Deletes an object by its identity.
95 // The identity to use to delete the object
96 var store = this.store;
97 this.store.fetchItemByIdentity({
99 onItem: function(item){
100 store.deleteItem(item);
104 query: function(query, options){
106 // Queries the store for objects.
108 // The query to use for retrieving objects from the store
110 // Optional options object as used by the underlying dojo.data Store.
111 // returns: dojo.store.util.QueryResults
112 // A query results object that can be used to iterate over results.
113 var returnedObject, returnedError;
114 var deferred = new dojo.Deferred();
115 deferred.total = new dojo.Deferred();
116 var converter = this._objectConverter(function(object){return object;});
117 this.store.fetch(dojo.mixin({
119 onBegin: function(count){
120 deferred.total.resolve(count);
122 onComplete: function(results){
123 deferred.resolve(dojo.map(results, converter));
125 onError: function(error){
126 deferred.reject(error);
129 return dojo.store.util.QueryResults(deferred);
131 getIdentity: function(object){
133 // Fetch the identity for the given object.
135 // The data object to get the identity from.
137 // The id of the given object.
138 return object[this.idProperty || this.store.getIdentityAttributes()[0]];