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

Слияние
author cin
date Fri, 08 Sep 2017 13:45:10 +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([ "dojo/_base/declare", "../safe" ], function(declare, safe) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
2 return declare(null, {
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 * Отображение одного типа объектов в другой.
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 * @remarks Отображения являются односторонними, т.е. позволяют
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
7 * перенести часть содержимого одного объекта в другой. Каждая
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 * отображений, которые будут применены в произвольном порядке.
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 _schema : 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 constructor : function(schema) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
14 this._schema = schema;
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
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 * Осуществляет отображение одного объекта в другой
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 * @src{Object} Исходный объект из которого будут взяты данные
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
21 * @dst{Object}
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 map : function(src, dst, ctx) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
24 safe.argumentNotNull(src, "src");
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
25 safe.argumentNotNull(dst, "dst");
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 for ( var p in this._schema) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
28 var mapper = this._schema[p];
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
29 if (mapper instanceof Function) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
30 dst[p] = mapper(src[p]);
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
31 } else if (mapper && mapper.map) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
32 mapper.map(src, dst, p, ctx);
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
33 } else {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
34 this._defaultMapper(src, dst, p, mapper, ctx);
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
35 }
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
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
39 _defaultMapper : function(src, dst, prop, opts) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
40 if (typeof (opts) == "string") {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
41 if (opts in src)
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
42 dst[prop] = src[opts];
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
43 } else if (opts && opts.type instanceof Function) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
44 if (src[prop] instanceof opts.type)
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
45 dst[prop] = src[prop];
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
46 else
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
47 dst[prop] = this._isPrimitiveType(opts.type) ? opts.type
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
48 .call(null, src[prop]) : new opts.type(src[prop]);
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 } else {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
51 if (!(prop in src))
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
52 if (opts && opts.required)
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
53 throw new Error("The " + prop + "is missing");
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
54 else
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
55 return;
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
56 dst[prop] = src[prop];
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 },
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 _isPrimitiveType : function(type) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
61 return (type === String || type === Number || type === Boolean
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
62 || type === Number || type === Date);
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 });
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
66
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
67 });