annotate core/src/js/data/declare-model.js @ 34:27e8e9e38e07 default tip

Слияние
author nickolay
date Wed, 05 Jun 2019 20:44:15 +0300
parents acdcdf1a8d21
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", "./_ModelBase", "./MapSchema" ], function(
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
2 declare, _ModelBase, MapSchema) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
3 /**
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
4 * Создает новый класс, унаследованный от ./ModelBase, с указанной схемой
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
5 * отображения данных.
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 * @details Модель представляет собой объект, живущий в рамках контекста
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 * в источнике данных (toObjectMap) и наооборот в модель хранения в
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
10 * источнике данных (fromObjectMap).
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 * <pre>
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 * name : 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 * age : Number, // при преобразоваении к объекту поле будет преобразовано dst.age = Number(src.age)
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 *
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
20 * age : [Number, null] // тоже самое что и age : Number
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 * date : [Date, function(v) { return v.toString() }] // указывается преобразование в одну и в другую сторону
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 * <pre>
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 return function(schema, mixins, opts) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
27 var fromObjectSchema = {}, toObjectSchema = {};
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
28 if (schema !== null && schema !== undefined) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
29 for ( var p in schema) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
30 var mapper = schema[p];
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 if (mapper instanceof Array) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
33 toObjectSchema[p] = mapper[0];
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
34 fromObjectSchema[p] = mapper[1];
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
35 } else {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
36 toObjectSchema[p] = mapper;
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
37 fromObjectSchema[p] = null;
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
38 }
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 if (arguments.length < 3) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
43 opts = mixins;
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
44 mixins = undefined;
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
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
47 var base = [ _ModelBase ];
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
48 if (mixins) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
49 if (mixins instanceof Array)
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
50 base = base.concat(mixins);
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
51 else
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
52 base.push(mixins);
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 var model = declare(base, opts);
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 model.toObjectMap = new MapSchema(toObjectSchema);
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
58
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
59 model.fromObjectMap = new MapSchema(fromObjectSchema);
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 model.readData = function(that, data, context) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
62 model.toObjectMap.map(data, that, context);
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
63 };
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 model.writeData = function(that, data, context) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
66 data = data || {};
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
67 model.fromObjectMap.map(that, data, context);
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 return model;
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 });