]>
git.wh0rd.org - tt-rss.git/blob - lib/dojo/store/Memory.js.uncompressed.js
5aeae33c71f4457349aaf8f42a8ce0e00f8d3525
1 define("dojo/store/Memory", ["../_base/declare", "./util/QueryResults", "./util/SimpleQueryEngine" /*=====, "./api/Store" =====*/],
2 function(declare
, QueryResults
, SimpleQueryEngine
/*=====, Store =====*/){
7 // No base class, but for purposes of documentation, the base class is dojo/store/api/Store
9 /*===== base = Store; =====*/
11 return declare("dojo.store.Memory", base
, {
13 // This is a basic in-memory object store. It implements dojo/store/api/Store.
14 constructor: function(options
){
16 // Creates a memory object store.
17 // options: dojo/store/Memory
18 // This provides any configuration information that will be mixed into the store.
19 // This should generally include the data property to provide the starting set of data.
20 for(var i
in options
){
23 this.setData(this.data
|| []);
26 // The array of all the objects in the memory store
30 // Indicates the property to use as the identity property. The values of this
31 // property should be unique.
35 // An index of data indices into the data array by id
38 // queryEngine: Function
39 // Defines the query engine to use for querying the data store
40 queryEngine
: SimpleQueryEngine
,
43 // Retrieves an object by its identity
45 // The identity to use to lookup the object
47 // The object in the store that matches the given id.
48 return this.data
[this.index
[id
]];
50 getIdentity: function(object
){
52 // Returns an object's identity
54 // The object to get the identity from
56 return object
[this.idProperty
];
58 put: function(object
, options
){
62 // The object to store.
63 // options: dojo/store/api/Store.PutDirectives?
64 // Additional metadata for storing the data. Includes an "id"
65 // property if a specific id is to be used.
69 idProperty
= this.idProperty
;
70 var id
= object
[idProperty
] = (options
&& "id" in options
) ? options
.id
: idProperty
in object
? object
[idProperty
] : Math
.random();
73 if(options
&& options
.overwrite
=== false){
74 throw new Error("Object already exists");
76 // replace the entry in data
77 data
[index
[id
]] = object
;
80 index
[id
] = data
.push(object
) - 1;
84 add: function(object
, options
){
86 // Creates an object, throws an error if the object already exists
88 // The object to store.
89 // options: dojo/store/api/Store.PutDirectives?
90 // Additional metadata for storing the data. Includes an "id"
91 // property if a specific id is to be used.
93 (options
= options
|| {}).overwrite
= false;
94 // call put with overwrite being false
95 return this.put(object
, options
);
99 // Deletes an object by its identity
101 // The identity to use to delete the object
103 // Returns true if an object was removed, falsy (undefined) if no object matched the id
104 var index
= this.index
;
105 var data
= this.data
;
107 data
.splice(index
[id
], 1);
108 // now we have to reindex
113 query: function(query
, options
){
115 // Queries the store for objects.
117 // The query to use for retrieving objects from the store.
118 // options: dojo/store/api/Store.QueryOptions?
119 // The optional arguments to apply to the resultset.
120 // returns: dojo/store/api/Store.QueryResults
121 // The results of the query, extended with iterative methods.
124 // Given the following store:
126 // | var store = new Memory({
128 // | {id: 1, name: "one", prime: false },
129 // | {id: 2, name: "two", even: true, prime: true},
130 // | {id: 3, name: "three", prime: true},
131 // | {id: 4, name: "four", even: true, prime: false},
132 // | {id: 5, name: "five", prime: true}
136 // ...find all items where "prime" is true:
138 // | var results = store.query({ prime: true });
140 // ...or find all items where "even" is true:
142 // | var results = store.query({ even: true });
143 return QueryResults(this.queryEngine(query
, options
)(this.data
));
145 setData: function(data
){
147 // Sets the given data as the source for this store, and indexes it
149 // An array of objects to use as the source of data.
151 // just for convenience with the data format IFRS expects
152 this.idProperty
= data
.identifier
;
153 data
= this.data
= data
.items
;
158 for(var i
= 0, l
= data
.length
; i
< l
; i
++){
159 this.index
[data
[i
][this.idProperty
]] = i
;