From faed0510c1a751fe3dc5347e61897756717ea7f4 Mon Sep 17 00:00:00 2001 From: Fabien Franzen Date: Tue, 19 Aug 2014 22:06:55 +0200 Subject: [PATCH] Coerce embedded model types --- lib/model.js | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/lib/model.js b/lib/model.js index dfcb0ef0..ee60ce72 100644 --- a/lib/model.js +++ b/lib/model.js @@ -189,6 +189,7 @@ ModelBaseClass.prototype._initProperties = function (data, options) { // Handle complex types (JSON/Object) var type = properties[p].type; if (!BASE_TYPES[type.name]) { + if (typeof self.__data[p] !== 'object' && self.__data[p]) { try { 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]); } } - 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) && self.__data[p] !== undefined && self.__data[p] !== null ) {