]>
git.wh0rd.org - tt-rss.git/blob - lib/dojo/store/Memory.js.uncompressed.js
1 define("dojo/store/Memory", ["../_base/declare", "./util/QueryResults", "./util/SimpleQueryEngine"], function(declare
, QueryResults
, SimpleQueryEngine
) {
5 // The module defines an in-memory object store.
8 return declare("dojo.store.Memory", null, {
10 // This is a basic in-memory object store. It implements dojo.store.api.Store.
11 constructor: function(/*dojo.store.Memory*/ options
){
13 // Creates a memory object store.
15 // This provides any configuration information that will be mixed into the store.
16 // This should generally include the data property to provide the starting set of data.
17 for(var i
in options
){
20 this.setData(this.data
|| []);
23 // The array of all the objects in the memory store
27 // Indicates the property to use as the identity property. The values of this
28 // property should be unique.
32 // An index of data indices into the data array by id
35 // queryEngine: Function
36 // Defines the query engine to use for querying the data store
37 queryEngine
: SimpleQueryEngine
,
40 // Retrieves an object by its identity
42 // The identity to use to lookup the object
44 // The object in the store that matches the given id.
45 return this.data
[this.index
[id
]];
47 getIdentity: function(object
){
49 // Returns an object's identity
51 // The object to get the identity from
53 return object
[this.idProperty
];
55 put: function(object
, options
){
59 // The object to store.
60 // options: dojo.store.api.Store.PutDirectives??
61 // Additional metadata for storing the data. Includes an "id"
62 // property if a specific id is to be used.
66 idProperty
= this.idProperty
;
67 var id
= (options
&& "id" in options
) ? options
.id
: idProperty
in object
? object
[idProperty
] : Math
.random();
70 if(options
&& options
.overwrite
=== false){
71 throw new Error("Object already exists");
73 // replace the entry in data
74 data
[index
[id
]] = object
;
77 index
[id
] = data
.push(object
) - 1;
81 add: function(object
, options
){
83 // Creates an object, throws an error if the object already exists
85 // The object to store.
86 // options: dojo.store.api.Store.PutDirectives??
87 // Additional metadata for storing the data. Includes an "id"
88 // property if a specific id is to be used.
90 (options
= options
|| {}).overwrite
= false;
91 // call put with overwrite being false
92 return this.put(object
, options
);
96 // Deletes an object by its identity
98 // The identity to use to delete the object
100 // Returns true if an object was removed, falsy (undefined) if no object matched the id
101 var index
= this.index
;
102 var data
= this.data
;
104 data
.splice(index
[id
], 1);
105 // now we have to reindex
110 query: function(query
, options
){
112 // Queries the store for objects.
114 // The query to use for retrieving objects from the store.
115 // options: dojo.store.api.Store.QueryOptions?
116 // The optional arguments to apply to the resultset.
117 // returns: dojo.store.api.Store.QueryResults
118 // The results of the query, extended with iterative methods.
121 // Given the following store:
123 // | var store = new dojo.store.Memory({
125 // | {id: 1, name: "one", prime: false },
126 // | {id: 2, name: "two", even: true, prime: true},
127 // | {id: 3, name: "three", prime: true},
128 // | {id: 4, name: "four", even: true, prime: false},
129 // | {id: 5, name: "five", prime: true}
133 // ...find all items where "prime" is true:
135 // | var results = store.query({ prime: true });
137 // ...or find all items where "even" is true:
139 // | var results = store.query({ even: true });
140 return QueryResults(this.queryEngine(query
, options
)(this.data
));
142 setData: function(data
){
144 // Sets the given data as the source for this store, and indexes it
146 // An array of objects to use as the source of data.
148 // just for convenience with the data format IFRS expects
149 this.idProperty
= data
.identifier
;
150 data
= this.data
= data
.items
;
155 for(var i
= 0, l
= data
.length
; i
< l
; i
++){
156 this.index
[data
[i
][this.idProperty
]] = i
;