annotate src/implab/data/StoreAdapter.js @ 1:93fb6c09f2e1

minor fixes
author cin
date Fri, 02 Jun 2017 18:15:22 +0300
parents fc2517695ee1
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([
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
2 "dojo/_base/declare",
1
93fb6c09f2e1 minor fixes
cin
parents: 0
diff changeset
3 "../safe",
0
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
4 "dojo/when",
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
5 "dojo/store/util/QueryResults" ],
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 function(declare, safe, when, QueryResults) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
8
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
9 "use strict";
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 /**
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 * для преобразования данных, например, отображения в списках элементов
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 */
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
16 return declare(null, {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
17 /**
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
18 * @type{String} Свойство, хранящее идентификатор
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 idProperty : null,
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
21
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
22 _store : null,
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
23
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
24 /**
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
25 * @param{String} opts.idProperty Имя свойства, в которое будет записан
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
26 * идентификатор, если не указан, то идентификатор будет
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
27 * взят из родительского хранилища или использоваться
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
28 * строка <code>id</code>
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
29 * @param{dojo.store} opts.store Родительское хранилище
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
30 */
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
31 constructor : function(opts) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
32 safe.argumentNotNull(opts, "opts");
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
33 safe.argumentNotNull(opts.store, "opts.store");
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
34
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
35 this._store = opts.store;
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
36 delete opts.store;
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
37 declare.safeMixin(this, opts);
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
38 this.idProperty = opts.idProperty || this._store.idProperty || "id";
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 getParentStore : function() {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
42 return this._store;
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
43 },
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 get : function(id) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
46 var me = this;
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
47 return when(me._store.get(id), function(x) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
48 var m = me.mapItem(x);
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
49 if (!(me.idProperty in m))
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
50 m[me.idProperty] = id;
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
51 return m;
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
52 });
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
53 },
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
54
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 * <code>translateQuery</code> для подготовки запроса, затем,
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
58 * <code>mapItem</code> для преобразования результатов.
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
59 */
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
60 query : function(q, options) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
61 var me = this, store = this._store;
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
62 return when(store.query(me.translateQuery(q), me
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
63 .translateOptions(options)), function(res) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
64 var total = res.total;
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
65 var mapped = res.map(function(x) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
66 var m = me.mapItem(x);
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
67 if (!(me.idProperty in m))
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
68 m[me.idProperty] = store.getIdentity &&
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
69 store.getIdentity(x);
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
70 return m;
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
71 });
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
72 mapped.total = total;
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
73 var results = new QueryResults(mapped);
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
74 console.log(results);
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
75 return results;
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
76 });
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 getIdentity : function(obj) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
80 return obj && obj[this.idProperty];
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
81 },
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 *
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
86 * @param{Object} q Запрос в формате текущего хранилища
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
87 * @returns{Object} Запрос в формате родительского хранилища
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
88 */
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
89 translateQuery : function(q) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
90 return q;
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 translateOptions : function(options) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
94 return options;
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
95 },
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
96
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 * в объекте можно задать идентификатор, иначе идентификатор будет
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 *
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
102 * @param{Object} item Объект из родительского хранилища
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
103 * @returns{Object} результат преобразования
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
104 */
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
105 mapItem : function(item) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
106 return item;
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
107 }
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 });