DataStore.js
2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
define(["doh", "dojo/store/DataStore", "dojo/data/ItemFileReadStore", "dojo/data/ItemFileWriteStore"],
function(doh, DataStore, ItemFileReadStore, ItemFileWriteStore){
var two = {id: 2, name: "two", even: true, prime: true},
four = {id: 4, name: "four", even: true, prime: false};
var dataStore = new ItemFileWriteStore({data:{
items: [
{id: 1, name: "one", prime: false},
{id: 2, name: "two", even: true, prime: true},
{id: 3, name: "three", prime: true},
{id: 4, name: "four", even: true, prime: false},
{id: 5, name: "five", prime: true,
children:[{_reference:1}, {_reference:2}, {_reference:3}]}
],
identifier:"id"
}});
dataStore.fetchItemByIdentity({identity:null});
var store = new DataStore({store:dataStore});
doh.register("dojo.tests.store.DataStore",
[
function testGet(t){
t.is(store.get(1).name, "one");
t.is(store.get(4).name, "four");
t.t(store.get(5).prime);
t.is(store.get(5).children[1].name, "two");
},
function testQuery1(t){
var d = new doh.Deferred();
store.query({prime: true}).then(d.getTestCallback(function(results){
t.is(results.length, 3);
t.is(results[2].children[2].name, "three");
}));
return d;
},
function testQuery2(t){
var d = new doh.Deferred();
var result = store.query({even: true});
result.map(d.getTestErrback(function(object){
for(var i in object){
t.is(object[i], (object.id == 2 ? two : four)[i], "map of " + i);
}
}));
result.then(d.getTestCallback(function(results){
t.is("four", results[1].name, "then");
}));
return d;
},
function testPutUpdate(t){
var four = store.get(4);
four.square = true;
store.put(four);
four = store.get(4);
t.t(four.square);
},
function testPutNew(t){
store.put({
id: 6,
perfect: true
});
t.t(store.get(6).perfect);
},
function testNoWriteFeature(t){
var readOnlyStore = new DataStore({store:new ItemFileReadStore({})});
t.f(readOnlyStore.put);
}
]
);
});