annotate src/utest/store/mock.js @ 27:aee8ea860e9b

added utest/store/mock - json based mock store
author cin
date Mon, 11 Dec 2017 16:48:19 +0300
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
27
aee8ea860e9b added utest/store/mock - json based mock store
cin
parents:
diff changeset
1 define([
aee8ea860e9b added utest/store/mock - json based mock store
cin
parents:
diff changeset
2 "dojo/_base/declare",
aee8ea860e9b added utest/store/mock - json based mock store
cin
parents:
diff changeset
3 "dojo/_base/lang",
aee8ea860e9b added utest/store/mock - json based mock store
cin
parents:
diff changeset
4 "dojo/request",
aee8ea860e9b added utest/store/mock - json based mock store
cin
parents:
diff changeset
5 "dojo/store/Memory",
aee8ea860e9b added utest/store/mock - json based mock store
cin
parents:
diff changeset
6 "dojo/Deferred",
aee8ea860e9b added utest/store/mock - json based mock store
cin
parents:
diff changeset
7 "dojo/store/util/QueryResults"
aee8ea860e9b added utest/store/mock - json based mock store
cin
parents:
diff changeset
8
aee8ea860e9b added utest/store/mock - json based mock store
cin
parents:
diff changeset
9 ], function (declare, lang, request, Memory, Deferred, QueryResults) {
aee8ea860e9b added utest/store/mock - json based mock store
cin
parents:
diff changeset
10 /**
aee8ea860e9b added utest/store/mock - json based mock store
cin
parents:
diff changeset
11 * @amdplugin
aee8ea860e9b added utest/store/mock - json based mock store
cin
parents:
diff changeset
12 * @usage
aee8ea860e9b added utest/store/mock - json based mock store
cin
parents:
diff changeset
13 *
aee8ea860e9b added utest/store/mock - json based mock store
cin
parents:
diff changeset
14 * <pre>
aee8ea860e9b added utest/store/mock - json based mock store
cin
parents:
diff changeset
15 * require([
aee8ea860e9b added utest/store/mock - json based mock store
cin
parents:
diff changeset
16 * &quot;tests/store/mock!./data/sample.json&quot;
aee8ea860e9b added utest/store/mock - json based mock store
cin
parents:
diff changeset
17 * ], function(Store) {
aee8ea860e9b added utest/store/mock - json based mock store
cin
parents:
diff changeset
18 * var store = new Store(); // will create a memory store
aee8ea860e9b added utest/store/mock - json based mock store
cin
parents:
diff changeset
19 * });
aee8ea860e9b added utest/store/mock - json based mock store
cin
parents:
diff changeset
20 * </pre>
aee8ea860e9b added utest/store/mock - json based mock store
cin
parents:
diff changeset
21 */
aee8ea860e9b added utest/store/mock - json based mock store
cin
parents:
diff changeset
22 var cache = {};
aee8ea860e9b added utest/store/mock - json based mock store
cin
parents:
diff changeset
23 return {
aee8ea860e9b added utest/store/mock - json based mock store
cin
parents:
diff changeset
24 load: function (id, require, callback) {
aee8ea860e9b added utest/store/mock - json based mock store
cin
parents:
diff changeset
25 var url = require.toUrl(id);
aee8ea860e9b added utest/store/mock - json based mock store
cin
parents:
diff changeset
26 if (url in cache) {
aee8ea860e9b added utest/store/mock - json based mock store
cin
parents:
diff changeset
27 callback(cache[url]);
aee8ea860e9b added utest/store/mock - json based mock store
cin
parents:
diff changeset
28 } else {
aee8ea860e9b added utest/store/mock - json based mock store
cin
parents:
diff changeset
29 request(url).then(function (data) {
aee8ea860e9b added utest/store/mock - json based mock store
cin
parents:
diff changeset
30 // handle result as text and parse it every time the
aee8ea860e9b added utest/store/mock - json based mock store
cin
parents:
diff changeset
31 // store
aee8ea860e9b added utest/store/mock - json based mock store
cin
parents:
diff changeset
32 // is created to get an independent copy
aee8ea860e9b added utest/store/mock - json based mock store
cin
parents:
diff changeset
33 var f = cache[url] = function (opts) {
aee8ea860e9b added utest/store/mock - json based mock store
cin
parents:
diff changeset
34 var store = new Memory(lang.mixin({
aee8ea860e9b added utest/store/mock - json based mock store
cin
parents:
diff changeset
35 data: JSON.parse(data),
aee8ea860e9b added utest/store/mock - json based mock store
cin
parents:
diff changeset
36 async: true,
aee8ea860e9b added utest/store/mock - json based mock store
cin
parents:
diff changeset
37 delay: 100
aee8ea860e9b added utest/store/mock - json based mock store
cin
parents:
diff changeset
38 }, opts || {}));
aee8ea860e9b added utest/store/mock - json based mock store
cin
parents:
diff changeset
39
aee8ea860e9b added utest/store/mock - json based mock store
cin
parents:
diff changeset
40 declare.safeMixin(store, {
aee8ea860e9b added utest/store/mock - json based mock store
cin
parents:
diff changeset
41 query: function () {
aee8ea860e9b added utest/store/mock - json based mock store
cin
parents:
diff changeset
42 var results = this.inherited(arguments);
aee8ea860e9b added utest/store/mock - json based mock store
cin
parents:
diff changeset
43 if (this.async) {
aee8ea860e9b added utest/store/mock - json based mock store
cin
parents:
diff changeset
44 var d = new Deferred();
aee8ea860e9b added utest/store/mock - json based mock store
cin
parents:
diff changeset
45 setTimeout(function () {
aee8ea860e9b added utest/store/mock - json based mock store
cin
parents:
diff changeset
46 d.resolve(results);
aee8ea860e9b added utest/store/mock - json based mock store
cin
parents:
diff changeset
47 }, this.delay);
aee8ea860e9b added utest/store/mock - json based mock store
cin
parents:
diff changeset
48 return new QueryResults(d.promise);
aee8ea860e9b added utest/store/mock - json based mock store
cin
parents:
diff changeset
49 }
aee8ea860e9b added utest/store/mock - json based mock store
cin
parents:
diff changeset
50 return results;
aee8ea860e9b added utest/store/mock - json based mock store
cin
parents:
diff changeset
51 },
aee8ea860e9b added utest/store/mock - json based mock store
cin
parents:
diff changeset
52
aee8ea860e9b added utest/store/mock - json based mock store
cin
parents:
diff changeset
53 get: function () {
aee8ea860e9b added utest/store/mock - json based mock store
cin
parents:
diff changeset
54 var results = this.inherited(arguments);
aee8ea860e9b added utest/store/mock - json based mock store
cin
parents:
diff changeset
55 if (this.async) {
aee8ea860e9b added utest/store/mock - json based mock store
cin
parents:
diff changeset
56 var d = new Deferred();
aee8ea860e9b added utest/store/mock - json based mock store
cin
parents:
diff changeset
57 setTimeout(function () {
aee8ea860e9b added utest/store/mock - json based mock store
cin
parents:
diff changeset
58 d.resolve(results);
aee8ea860e9b added utest/store/mock - json based mock store
cin
parents:
diff changeset
59 }, this.delay);
aee8ea860e9b added utest/store/mock - json based mock store
cin
parents:
diff changeset
60 return d.promise;
aee8ea860e9b added utest/store/mock - json based mock store
cin
parents:
diff changeset
61 }
aee8ea860e9b added utest/store/mock - json based mock store
cin
parents:
diff changeset
62 return results;
aee8ea860e9b added utest/store/mock - json based mock store
cin
parents:
diff changeset
63 },
aee8ea860e9b added utest/store/mock - json based mock store
cin
parents:
diff changeset
64
aee8ea860e9b added utest/store/mock - json based mock store
cin
parents:
diff changeset
65 put: function () {
aee8ea860e9b added utest/store/mock - json based mock store
cin
parents:
diff changeset
66 var me = this;
aee8ea860e9b added utest/store/mock - json based mock store
cin
parents:
diff changeset
67
aee8ea860e9b added utest/store/mock - json based mock store
cin
parents:
diff changeset
68 if (me.async) {
aee8ea860e9b added utest/store/mock - json based mock store
cin
parents:
diff changeset
69 var inherited = me.getInherited(arguments);
aee8ea860e9b added utest/store/mock - json based mock store
cin
parents:
diff changeset
70 var args = Array.prototype.slice.apply(arguments);
aee8ea860e9b added utest/store/mock - json based mock store
cin
parents:
diff changeset
71 var d = new Deferred();
aee8ea860e9b added utest/store/mock - json based mock store
cin
parents:
diff changeset
72
aee8ea860e9b added utest/store/mock - json based mock store
cin
parents:
diff changeset
73 setTimeout(function () {
aee8ea860e9b added utest/store/mock - json based mock store
cin
parents:
diff changeset
74 d.resolve(inherited.apply(me, args));
aee8ea860e9b added utest/store/mock - json based mock store
cin
parents:
diff changeset
75 }, me.delay);
aee8ea860e9b added utest/store/mock - json based mock store
cin
parents:
diff changeset
76
aee8ea860e9b added utest/store/mock - json based mock store
cin
parents:
diff changeset
77 return d.promise;
aee8ea860e9b added utest/store/mock - json based mock store
cin
parents:
diff changeset
78 }
aee8ea860e9b added utest/store/mock - json based mock store
cin
parents:
diff changeset
79 return me.inherited(arguments);
aee8ea860e9b added utest/store/mock - json based mock store
cin
parents:
diff changeset
80 }
aee8ea860e9b added utest/store/mock - json based mock store
cin
parents:
diff changeset
81 });
aee8ea860e9b added utest/store/mock - json based mock store
cin
parents:
diff changeset
82
aee8ea860e9b added utest/store/mock - json based mock store
cin
parents:
diff changeset
83 return store;
aee8ea860e9b added utest/store/mock - json based mock store
cin
parents:
diff changeset
84 };
aee8ea860e9b added utest/store/mock - json based mock store
cin
parents:
diff changeset
85 callback(f);
aee8ea860e9b added utest/store/mock - json based mock store
cin
parents:
diff changeset
86 }, function (err) {
aee8ea860e9b added utest/store/mock - json based mock store
cin
parents:
diff changeset
87 require.signal("error", [{
aee8ea860e9b added utest/store/mock - json based mock store
cin
parents:
diff changeset
88 error: err,
aee8ea860e9b added utest/store/mock - json based mock store
cin
parents:
diff changeset
89 src: 'implab/store/mock'
aee8ea860e9b added utest/store/mock - json based mock store
cin
parents:
diff changeset
90 }]);
aee8ea860e9b added utest/store/mock - json based mock store
cin
parents:
diff changeset
91 });
aee8ea860e9b added utest/store/mock - json based mock store
cin
parents:
diff changeset
92 }
aee8ea860e9b added utest/store/mock - json based mock store
cin
parents:
diff changeset
93 }
aee8ea860e9b added utest/store/mock - json based mock store
cin
parents:
diff changeset
94 };
aee8ea860e9b added utest/store/mock - json based mock store
cin
parents:
diff changeset
95 });