Coerce embedded model types

This commit is contained in:
Fabien Franzen 2014-08-19 22:06:55 +02:00 committed by Raymond Feng
parent 3b81b928fb
commit faed0510c1
1 changed files with 9 additions and 1 deletions

View File

@ -189,6 +189,7 @@ ModelBaseClass.prototype._initProperties = function (data, options) {
// Handle complex types (JSON/Object) // Handle complex types (JSON/Object)
var type = properties[p].type; var type = properties[p].type;
if (!BASE_TYPES[type.name]) { if (!BASE_TYPES[type.name]) {
if (typeof self.__data[p] !== 'object' && self.__data[p]) { if (typeof self.__data[p] !== 'object' && self.__data[p]) {
try { try {
self.__data[p] = JSON.parse(self.__data[p] + ''); self.__data[p] = JSON.parse(self.__data[p] + '');
@ -196,7 +197,14 @@ ModelBaseClass.prototype._initProperties = function (data, options) {
self.__data[p] = String(self.__data[p]); self.__data[p] = String(self.__data[p]);
} }
} }
if (type.name === 'Array' || Array.isArray(type)) {
if (type.prototype instanceof ModelBaseClass) {
if (!(self.__data[p] instanceof type)
&& typeof self.__data[p] === 'object'
&& self.__data[p] !== null ) {
self.__data[p] = new type(self.__data[p]);
}
} else if (type.name === 'Array' || Array.isArray(type)) {
if (!(self.__data[p] instanceof List) if (!(self.__data[p] instanceof List)
&& self.__data[p] !== undefined && self.__data[p] !== undefined
&& self.__data[p] !== null ) { && self.__data[p] !== null ) {