]>
git.wh0rd.org - tt-rss.git/blob - 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
) {
4 // dojo/store/DataStore
9 return declare("dojo.store.DataStore", null, {
11 constructor: function(options
){
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
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
){
23 idAttribute
= this.store
.getIdentityAttributes();
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.
28 // if no idAttribute we have implicit id
29 this.idProperty
= (!idAttribute
|| !idAttributes
[0]) || this.idProperty
;
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"]){
36 if(!features
["dojo.data.api.Identity"]){
37 this.getIdentity
= null;
39 if(!features
["dojo.data.api.Write"]){
40 this.put
= this.add
= null;
44 // The object property to use to store the identity of the store items.
47 // The object store to convert to a data store
49 _objectConverter: function(callback
){
50 var store
= this.store
;
51 var idProperty
= this.idProperty
;
52 return function(item
){
54 var attributes
= store
.getAttributes(item
);
55 for(var i
= 0; i
< attributes
.length
; i
++){
56 object
[attributes
[i
]] = store
.getValue(item
, attributes
[i
]);
58 if(!(idProperty
in object
)){
59 object
[idProperty
] = store
.getIdentity(item
);
61 return callback(object
);
64 get: function(id
, options
){
66 // Retrieves an object by it's identity. This will trigger a fetchItemByIdentity
68 // The identity to use to lookup the object
69 var returnedObject
, returnedError
;
70 var deferred
= new Deferred();
71 this.store
.fetchItemByIdentity({
73 onItem
: this._objectConverter(function(object
){
74 deferred
.resolve(returnedObject
= object
);
76 onError: function(error
){
77 deferred
.reject(returnedError
= error
);
81 // if it was returned synchronously
82 return returnedObject
;
87 return deferred
.promise
;
89 put: function(object
, options
){
91 // Stores an object by its identity.
93 // The object to store.
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
);
103 store
.fetchItemByIdentity({
105 onItem: function(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
]);
114 store
.newItem(object
);
120 remove: function(id
){
122 // Deletes an object by its identity.
124 // The identity to use to delete the object
125 var store
= this.store
;
126 this.store
.fetchItemByIdentity({
128 onItem: function(item
){
129 store
.deleteItem(item
);
133 query: function(query
, options
){
135 // Queries the store for objects.
137 // The query to use for retrieving objects from the store
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.
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({
148 onBegin: function(count
){
149 deferred
.total
.resolve(count
);
151 onComplete: function(results
){
152 deferred
.resolve(array
.map(results
, converter
));
154 onError: function(error
){
155 deferred
.reject(error
);
158 return QueryResults(deferred
);
160 getIdentity: function(object
){
162 // Fetch the identity for the given object.
164 // The data object to get the identity from.
166 // The id of the given object.
167 return object
[this.idProperty
];