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