]>
Commit | Line | Data |
---|---|---|
1354d172 AD |
1 | define("dojo/store/Memory", ["../_base/declare", "./util/QueryResults", "./util/SimpleQueryEngine"], function(declare, QueryResults, SimpleQueryEngine) { |
2 | // module: | |
3 | // dojo/store/Memory | |
4 | // summary: | |
5 | // The module defines an in-memory object store. | |
6 | ||
7 | ||
8 | return declare("dojo.store.Memory", null, { | |
9 | // summary: | |
10 | // This is a basic in-memory object store. It implements dojo.store.api.Store. | |
11 | constructor: function(/*dojo.store.Memory*/ options){ | |
12 | // summary: | |
13 | // Creates a memory object store. | |
14 | // options: | |
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){ | |
18 | this[i] = options[i]; | |
19 | } | |
20 | this.setData(this.data || []); | |
21 | }, | |
22 | // data: Array | |
23 | // The array of all the objects in the memory store | |
24 | data:null, | |
25 | ||
26 | // idProperty: String | |
27 | // Indicates the property to use as the identity property. The values of this | |
28 | // property should be unique. | |
29 | idProperty: "id", | |
30 | ||
31 | // index: Object | |
32 | // An index of data indices into the data array by id | |
33 | index:null, | |
34 | ||
35 | // queryEngine: Function | |
36 | // Defines the query engine to use for querying the data store | |
37 | queryEngine: SimpleQueryEngine, | |
38 | get: function(id){ | |
39 | // summary: | |
40 | // Retrieves an object by its identity | |
41 | // id: Number | |
42 | // The identity to use to lookup the object | |
43 | // returns: Object | |
44 | // The object in the store that matches the given id. | |
45 | return this.data[this.index[id]]; | |
46 | }, | |
47 | getIdentity: function(object){ | |
48 | // summary: | |
49 | // Returns an object's identity | |
50 | // object: Object | |
51 | // The object to get the identity from | |
52 | // returns: Number | |
53 | return object[this.idProperty]; | |
54 | }, | |
55 | put: function(object, options){ | |
56 | // summary: | |
57 | // Stores an object | |
58 | // object: Object | |
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. | |
63 | // returns: Number | |
64 | var data = this.data, | |
65 | index = this.index, | |
66 | idProperty = this.idProperty; | |
67 | var id = (options && "id" in options) ? options.id : idProperty in object ? object[idProperty] : Math.random(); | |
68 | if(id in index){ | |
69 | // object exists | |
70 | if(options && options.overwrite === false){ | |
71 | throw new Error("Object already exists"); | |
72 | } | |
73 | // replace the entry in data | |
74 | data[index[id]] = object; | |
75 | }else{ | |
76 | // add the new object | |
77 | index[id] = data.push(object) - 1; | |
78 | } | |
79 | return id; | |
80 | }, | |
81 | add: function(object, options){ | |
82 | // summary: | |
83 | // Creates an object, throws an error if the object already exists | |
84 | // object: Object | |
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. | |
89 | // returns: Number | |
90 | (options = options || {}).overwrite = false; | |
91 | // call put with overwrite being false | |
92 | return this.put(object, options); | |
93 | }, | |
94 | remove: function(id){ | |
95 | // summary: | |
96 | // Deletes an object by its identity | |
97 | // id: Number | |
98 | // The identity to use to delete the object | |
99 | // returns: Boolean | |
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; | |
103 | if(id in index){ | |
104 | data.splice(index[id], 1); | |
105 | // now we have to reindex | |
106 | this.setData(data); | |
107 | return true; | |
108 | } | |
109 | }, | |
110 | query: function(query, options){ | |
111 | // summary: | |
112 | // Queries the store for objects. | |
113 | // query: Object | |
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. | |
119 | // | |
120 | // example: | |
121 | // Given the following store: | |
122 | // | |
123 | // | var store = new dojo.store.Memory({ | |
124 | // | data: [ | |
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} | |
130 | // | ] | |
131 | // | }); | |
132 | // | |
133 | // ...find all items where "prime" is true: | |
134 | // | |
135 | // | var results = store.query({ prime: true }); | |
136 | // | |
137 | // ...or find all items where "even" is true: | |
138 | // | |
139 | // | var results = store.query({ even: true }); | |
140 | return QueryResults(this.queryEngine(query, options)(this.data)); | |
141 | }, | |
142 | setData: function(data){ | |
143 | // summary: | |
144 | // Sets the given data as the source for this store, and indexes it | |
145 | // data: Object[] | |
146 | // An array of objects to use as the source of data. | |
147 | if(data.items){ | |
148 | // just for convenience with the data format IFRS expects | |
149 | this.idProperty = data.identifier; | |
150 | data = this.data = data.items; | |
151 | }else{ | |
152 | this.data = data; | |
153 | } | |
154 | this.index = {}; | |
155 | for(var i = 0, l = data.length; i < l; i++){ | |
156 | this.index[data[i][this.idProperty]] = i; | |
157 | } | |
158 | } | |
159 | }); | |
160 | ||
161 | }); |