##// END OF EJS Templates
Added childContainer service to container services, ServiceContaier is destroyable, fixed browser Uuid version
Added childContainer service to container services, ServiceContaier is destroyable, fixed browser Uuid version

File last commit:

r51:43a2828f8abe v1.2.0-rc di-typescript
r146:f3f5c56d3b3e v1.4.0-rc5 default
Show More
MapSchema.js
66 lines | 2.6 KiB | application/javascript | JavascriptLexer
cin
working version...
r51 define([ "dojo/_base/declare", "../safe" ], function(declare, safe) {
return declare(null, {
/**
* Отображение одного типа объектов в другой.
*
* @remarks Отображения являются односторонними, т.е. позволяют
* перенести часть содержимого одного объекта в другой. Каждая
* схема отображения строится из набора примитивных
* отображений, которые будут применены в произвольном порядке.
*/
_schema : null,
constructor : function(schema) {
this._schema = schema;
},
/**
* Осуществляет отображение одного объекта в другой
*
* @src{Object} Исходный объект из которого будут взяты данные
* @dst{Object}
*/
map : function(src, dst, ctx) {
safe.argumentNotNull(src, "src");
safe.argumentNotNull(dst, "dst");
for ( var p in this._schema) {
var mapper = this._schema[p];
if (mapper instanceof Function) {
dst[p] = mapper(src[p]);
} else if (mapper && mapper.map) {
mapper.map(src, dst, p, ctx);
} else {
this._defaultMapper(src, dst, p, mapper, ctx);
}
}
},
_defaultMapper : function(src, dst, prop, opts) {
if (typeof (opts) == "string") {
if (opts in src)
dst[prop] = src[opts];
} else if (opts && opts.type instanceof Function) {
if (src[prop] instanceof opts.type)
dst[prop] = src[prop];
else
dst[prop] = this._isPrimitiveType(opts.type) ? opts.type
.call(null, src[prop]) : new opts.type(src[prop]);
} else {
if (!(prop in src))
if (opts && opts.required)
throw new Error("The " + prop + "is missing");
else
return;
dst[prop] = src[prop];
}
},
_isPrimitiveType : function(type) {
return (type === String || type === Number || type === Boolean
|| type === Number || type === Date);
}
});
});