]> git.wh0rd.org Git - tt-rss.git/blob - lib/dojo/store/DataStore.js
upgrade Dojo to 1.6.1
[tt-rss.git] / lib / dojo / store / DataStore.js
1 /*
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
5 */
6
7
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");
12
13
14 dojo.declare("dojo.store.DataStore", null, {
15         target: "",
16         constructor: function(options){
17                 // summary:
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
21                 // options: Object?
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);
25         },
26         _objectConverter: function(callback){
27                 var store = this.store;
28                 return function(item){
29                         var object = {};
30                         var attributes = store.getAttributes(item);
31                         for(var i = 0; i < attributes.length; i++){
32                                 object[attributes[i]] = store.getValue(item, attributes[i]);
33                         }
34                         return callback(object);
35                 };
36         },
37         get: function(id, options){
38                 // summary:
39                 //              Retrieves an object by it's identity. This will trigger a fetchItemByIdentity
40                 // id: Object?
41                 //              The identity to use to lookup the object
42                 var returnedObject, returnedError;
43                 var deferred = new dojo.Deferred();
44                 this.store.fetchItemByIdentity({
45                         identity: id,
46                         onItem: this._objectConverter(function(object){
47                                 deferred.resolve(returnedObject = object);
48                         }),
49                         onError: function(error){
50                                 deferred.reject(returnedError = error);
51                         }
52                 });
53                 if(returnedObject){
54                         // if it was returned synchronously
55                         return returnedObject;
56                 }
57                 if(returnedError){
58                         throw returnedError;
59                 }
60                 return deferred.promise;
61         },
62         put: function(object, options){
63                 // summary:
64                 //              Stores an object by its identity.
65                 // object: Object
66                 //              The object to store.
67                 // options: Object?
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);
74                 }else{
75                         store.fetchItemByIdentity({
76                                 identity: id,
77                                 onItem: function(item){
78                                         if(item){
79                                                 for(var i in object){
80                                                         if(store.getValue(item, i) != object[i]){
81                                                                 store.setValue(item, i, object[i]);
82                                                         }
83                                                 }
84                                         }else{
85                                                 store.newItem(object);
86                                         }
87                                 }
88                         });
89                 }
90         },
91         remove: function(id){
92                 // summary:
93                 //              Deletes an object by its identity.
94                 // id: Object
95                 //              The identity to use to delete the object
96                 var store = this.store;
97                 this.store.fetchItemByIdentity({
98                         identity: id,
99                         onItem: function(item){
100                                 store.deleteItem(item);
101                         }
102                 });
103         },
104         query: function(query, options){
105                 // summary:
106                 //              Queries the store for objects.
107                 // query: Object
108                 //              The query to use for retrieving objects from the store
109                 // options: Object?
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({
118                         query: query,
119                         onBegin: function(count){
120                                 deferred.total.resolve(count);
121                         },
122                         onComplete: function(results){
123                                 deferred.resolve(dojo.map(results, converter));
124                         },
125                         onError: function(error){
126                                 deferred.reject(error);
127                         }
128                 }, options));
129                 return dojo.store.util.QueryResults(deferred);
130         },
131         getIdentity: function(object){
132                 // summary:
133                 //              Fetch the identity for the given object.
134                 // object: Object
135                 //              The data object to get the identity from.
136                 // returns: Number
137                 //              The id of the given object.
138                 return object[this.idProperty || this.store.getIdentityAttributes()[0]];
139         }
140 });
141
142 }