annotate src/implab/data/ObjectStore.js @ 14:5fd2a35d65c0

Слияние
author cin
date Fri, 08 Sep 2017 13:45:10 +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 model: 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 mapping: 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 _dataContext: null,
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
18
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
19 _store : null, // backing store
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
20
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
21 _cache : null,
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 constructor : function(options) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
24 options = options || {};
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
25
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
26 if (options.store)
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
27 this._store = options.store;
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 if (options.dataContext) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
30 this._dataContext = options.dataContext;
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
31 }
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 if (options.cache === false) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
34 // no cache at all
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
35 } else if (options.cache === "string" && options.dataContext) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
36 this._cache = this._dataContext.getCache(options.cache);
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
37 } else {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
38 this._cache = {};
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
39 }
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
40 },
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
41
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
42 getDataContext : function() {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
43 return this._dataContext;
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
44 },
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
45
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
46 // READ
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
47 get : function(id) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
48 var me = this;
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
49 var cache = me.getCacheEntry(id);
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
50 if (cache)
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
51 return cache;
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
52 else
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
53 return when(me._store.get(id), function(data) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
54 return me._mapToObject(id, data);
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
55 });
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 query : function(query, options) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
59 var me = this;
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
60 var d = me._store.query(query, options);
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
61 var result = QueryResults(when(d, function(data) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
62 return array.map(data, function(item) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
63 return me._mapToObject(me._store.getIdentity(item), item);
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 result.total = d.total;
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
67 return result;
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
68 },
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 getIdentity : function(object) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
71 return object.getId();
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
72 },
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
73
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
74 // UPDATE
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
75 put : function(object, directives) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
76 return this._store.put(this._mapFromObject(object), directives);
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
77 },
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
78
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
79 // INSERT
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
80 add : function(object, directives) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
81 var me = this;
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 return when(
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
85 me._store.add(this._mapFromObject(object), directives),
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
86 function(id) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
87 object.attach(id, me);
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
88 me.storeCacheEntry(id, object);
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
89 return 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 // DELETE
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
94 remove : function(id) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
95 var me = this;
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
96 return when(me._store.remove(id), function() {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
97 me.removeCacheEntry(id);
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 },
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
100
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
101 _mapToObject : function(id, data) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
102 var instance = this.createInstance(id);
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
103 this.populateInstance(instance, data);
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
104 return instance;
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
105 },
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 _mapFromObject : function(object) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
108 return this.serializeInstance(object);
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
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
111 getCacheEntry : function(id) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
112 safe.argumentNotNull(id, "id");
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
113 id = id.toString();
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
114
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
115 return this._cache[id];
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
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
118 storeCacheEntry : function(id, object) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
119 safe.argumentNotNull(id, "id");
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
120 id = id.toString();
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 this._cache[id] = object;
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
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
125 removeCacheEntry : function(id) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
126 safe.argumentNotNull(id, "id");
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
127 id = id.toString();
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
128 delete this._cache[id];
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 * @remarks
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
133 * Технически сюда можно было бы дополнительно передать данные для ининциализации объекта,
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
134 * но концептуально это не верно, поскольку процесс чтения объекта состоит из двух этапов:
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
135 * 1. Создание пустого объекта (createInstance)
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
136 * 2. Заполнение объекта при помощи схемы отображения (populateInstance)
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 * при создании заглушек в процессе установления ссылок между объектами.
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 createInstance : function(id) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
141 var instance = this.getCacheEntry(id);
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
142 if (!instance) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
143 instance = this.createInstanceImpl(id);
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
144 this.storeCacheEntry(id, instance);
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
145 }
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
146 return instance;
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
147 },
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 * @param {String} id идентификатор создаваемого экземпляра.
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 createInstanceImpl : function(id) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
153 var opts = {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
154 dataContext : this.getDataContext(),
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
155 id : id
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
156 };
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 return new this.itemsType(opts);
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
159 },
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
160
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
161 populateInstance : function(instance, data) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
162 this.mapping.readData(instance, data,this.getDataContext());
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
163 if (instance.onPopulate)
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
164 instance.onPopulate();
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
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
167 serializeInstance : function(instance) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
168 var data = {};
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
169 this.mapping.writeData(instance, data, this.getDataContext());
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
170 return data;
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
171 }
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
172
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
173 });
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
174 });