annotate src/implab/data/RestStore.js @ 20:9718e8de0cb2

Слияние
author nickolay
date Fri, 06 Oct 2017 09:17:41 +0300
parents f0035923ff3e
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
1 define([ "dojo/_base/declare", "dojo/_base/lang", "dojo/_base/array",
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
2 "../safe", "dojo/when", "dojo/Deferred", "dojo/store/util/QueryResults" ], function(declare,
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
3 lang, array, safe, when, Deferred, QueryResults) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
4 /**
8
f0035923ff3e добавлена библиотека для работы с openlayers 3+
cin
parents: 0
diff changeset
5 * @module implab/data/RestStore
0
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
6 *
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
7 * Реализует шаблон репозитария dojo/store над уже имеющимся хранилищем. При получении и
8
f0035923ff3e добавлена библиотека для работы с openlayers 3+
cin
parents: 0
diff changeset
8 * отправке данных в нижележащие хранилище используется implab/data/MapSchema для преобразования
0
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
9 * данных.
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
10 */
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
11 return declare(null, {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
12
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
13 itemsType : null,
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
14
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
15 _dataContext : null,
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
16
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
17 _store : null, // backing store
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
18 _cache : null,
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
19
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
20 constructor : function(options) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
21 options = options || {};
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
22
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
23 this._cache = {};
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
24 if (options.store)
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
25 this._store = options.store;
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
26 if (options.dataContext)
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
27 this._dataContext = options.dataContext;
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
28 },
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
29
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
30 setDataContext : function(v) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
31 this._dataContext = v;
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
32 },
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
33
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
34 getDataContext : function() {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
35 return this._dataContext;
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
36 },
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
37
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
38 // READ
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
39 get : function(id) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
40 var me = this;
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
41 var cache = me.getCacheEntry(id);
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
42 if (cache)
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
43 return cache;
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
44 else
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
45 return when(me._store.get(id), function(data) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
46 return me._mapToObject(id, data);
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
47 });
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
48 },
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
49
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
50 query : function(query, options) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
51 var me = this;
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
52 var d = me._store.query(query, options);
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
53 var result = QueryResults(when(d, function(data) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
54 return array.map(data, function(item) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
55 return me._mapToObject(me._store.getIdentity(item), item);
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
56 });
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
57 }));
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
58 result.total = d.total;
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
59 return result;
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
60 },
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
61
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
62 getIdentity : function(object) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
63 return object.getId();
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
64 },
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
65
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
66 // UPDATE
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
67 put : function(object, directives) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
68 return this._store.put(this._mapFromObject(object), directives);
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
69 },
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
70
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
71 // INSERT
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
72 add : function(object, directives) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
73 var me = this;
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
74 // добавляем в хранилище данные, сохраняем в кеше объект с
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
75 // полученным идентификатором
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
76 return when(
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
77 me._store.add(this._mapFromObject(object), directives),
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
78 function(id) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
79 object.attach(id, me);
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
80 me.storeCacheEntry(id, object);
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
81 return id;
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
82 });
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
83 },
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
84
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
85 // DELETE
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
86 remove : function(id) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
87 var me = this;
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
88 return when(me._store.remove(id), function() {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
89 me.removeCacheEntry(id);
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
90 });
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
91 },
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
92
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
93 _mapToObject : function(id, data) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
94 var instance = this.createInstance(id);
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
95 this.populateInstance(instance, data);
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
96 return instance;
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
97 },
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
98
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
99 _mapFromObject : function(object) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
100 return this.serializeInstance(object);
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
101 },
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
102
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
103 getCacheEntry : function(id) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
104 safe.argumentNotNull(id, "id");
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
105 id = id.toString();
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
106
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
107 return this._cache[id];
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
108 },
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
109
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
110 storeCacheEntry : function(id, object) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
111 safe.argumentNotNull(id, "id");
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
112 id = id.toString();
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
113
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
114 this._cache[id] = object;
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
115 },
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
116
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
117 removeCacheEntry : function(id) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
118 safe.argumentNotNull(id, "id");
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
119 id = id.toString();
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
120 delete this._cache[id];
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
121 },
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
122
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
123 /** Создает экземпляр сущности с указанным идентификатором, либо извлекает из кеша, если таковая уже имеется.
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
124 * @remarks
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
125 * Технически сюда можно было бы дополнительно передать данные для ининциализации объекта,
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
126 * но концептуально это не верно, поскольку процесс чтения объекта состоит из двух этапов:
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
127 * 1. Создание пустого объекта (createInstance)
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
128 * 2. Заполнение объекта при помощи схемы отображения (populateInstance)
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
129 * при этом первый этап может быть выполнен за долго до второго, например,
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
130 * при создании заглушек в процессе установления ссылок между объектами.
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
131 */
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
132 createInstance : function(id) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
133 var instance = this.getCacheEntry(id);
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
134 if (!instance) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
135 instance = this.createInstanceImpl(id);
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
136 this.storeCacheEntry(id, instance);
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
137 }
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
138 return instance;
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
139 },
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
140
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
141 /** Непосредственно создает экземпляр сущнсти, т.е. является фабричным методом.
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
142 * @param {String} id идентификатор создаваемого экземпляра.
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
143 */
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
144 createInstanceImpl : function(id) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
145 var opts = {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
146 dataContext : this.getDataContext(),
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
147 id : id
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
148 };
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
149
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
150 return new this.itemsType(opts);
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
151 },
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
152
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
153 populateInstance : function(instance, data) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
154 this.itemsType.readData(instance, data,this.getDataContext());
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
155 if (instance.onPopulate)
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
156 instance.onPopulate();
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
157 },
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
158
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
159 serializeInstance : function(instance) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
160 var data = {};
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
161 this.itemsType.writeData(instance, data, this.getDataContext());
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
162 return data;
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
163 }
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
164
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
165 });
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
166 });