jxdnsong
2020-10-23 a7929e6b3ec9ac17233f39e55a2b8ac63ea75f42
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title>Test OnDemandGrids with promise-based stores</title>
        <link rel="stylesheet" href="../../dojo/resources/dojo.css">
        <link rel="stylesheet" href="../css/dgrid.css">
        <link rel="stylesheet" href="../css/skins/claro.css">
        <style>
            .dgrid {
                height: 20em;
                margin: 1em;
            }
        </style>
    </head>
 
    <body class="claro">
        <p>This test page tests stores that return promises from their query methods,
            and old stores wrapped with adapters.</p>
 
        <div>
            <button onclick="filter(true)">Show only items named 'foo'</button>
            <button onclick="filter(false)">Reset query</button>
        </div>
 
        <h2>Grid with a dstore/Memory store</h2>
        <div id="grid1"></div>
 
        <h2>Grid with a dojo/store</h2>
        <div id="grid2"></div>
 
        <h2>Grid with a dojo/data store</h2>
        <div id="grid3"></div>
 
        <h2>Grid with a dstore/RequestMemory store</h2>
        <div id="grid4"></div>
 
<script src="../../dojo/dojo.js" data-dojo-config="async: true"></script>
 
<script>
var filter;
 
require([
    'dojo/_base/declare',
    'dojo/_base/lang',
    'dojo/Deferred',
    'dojo/data/ItemFileReadStore',
    'dojo/store/DataStore',
    'dojo/store/Memory',
    'dojo/store/util/QueryResults',
    'dgrid/OnDemandGrid',
    'dgrid/test/data/createAsyncStore',
    'dstore/RequestMemory',
    'dstore/legacy/StoreAdapter',
    'dstore/Trackable'
], function (declare, lang, Deferred, ItemFileReadStore, DataStore, LegacyMemory, QueryResults, OnDemandGrid,
    createAsyncStore, RequestMemory, StoreAdapter, Trackable) {
 
    var dataStore;
    var names = [ 'sasquatch', 'foo', 'bar', 'zaphod', 'beeblebrox' ];
    var grids = [];
    var stores = [];
 
    function createItems () {
        // generate data
        var items = [];
        var i;
        for (i = 1; i <= 200; i++) {
            items.push({
                id: i,
                name: names[i % 5]
            });
        }
        return items;
    }
 
    filter = function (doFilter) {
        for (var i = grids.length; i--;) {
            grids[i].set('collection',
                doFilter ? stores[i].filter({ name: 'foo' }) : stores[i]);
        }
    };
 
    stores.push(createAsyncStore({ data: createItems() }));
 
    // Create a grid with an async dstore store
    grids.push(new OnDemandGrid({
        columns: { id: 'id', name: 'name' },
        collection: stores[0]
    }, 'grid1'));
 
    // Create a grid with an async dojo/store store
    var LegacyAsyncMemory = declare(LegacyMemory, {
        query: function (query, options) {
            var results = this.inherited(arguments);
            var def = new Deferred(function () {
                clearTimeout(timer);
            });
            var timer = setTimeout(function () {
                def.resolve(results);
            }, 200);
            var queryResults = new QueryResults(def.promise);
            queryResults.total = this.data.length;
            return queryResults;
        }
    });
 
    // Create a dojo/store instance and use the dstore adapter
    stores.push(new declare([ StoreAdapter, Trackable ])({
        objectStore: new LegacyAsyncMemory({
            data: createItems()
        })
    }));
 
    grids.push(new OnDemandGrid({
        columns: { id: 'id', name: 'name' },
        collection: stores[1]
    }, 'grid2'));
 
    // Create a dojo/data store instance and use the dojo/store/DataStore and dstore adapters
    stores.push(new StoreAdapter({
        objectStore: new DataStore({
            store: new ItemFileReadStore({
                data: {
                    identifier: 'id',
                    items: createItems()
                }
            })
        })
    }));
 
    grids.push(new OnDemandGrid({
        columns: { id: 'id', name: 'name' },
        collection: stores[2]
    }, 'grid3'));
    
    // Create a dstore RequestMemory instance
    stores.push(new RequestMemory({ target: 'data/requestData.json' }));
    grids.push(new OnDemandGrid({
        columns: { id: 'id', name: 'name' },
        collection: stores[3]
    }, 'grid4'));
});
</script>
 
</body>
</html>