diff --git a/lib/dao.js b/lib/dao.js index e7d08012..1c18853c 100644 --- a/lib/dao.js +++ b/lib/dao.js @@ -230,7 +230,7 @@ DataAccessObject.upsert = DataAccessObject.updateOrCreate = function upsert(data this.dataSource.connector.updateOrCreate(Model.modelName, inst.toObject(true), function (err, data) { var obj; if (data) { - inst._initProperties(data, true); + inst._initProperties(data, false); obj = inst; } else { obj = null; @@ -327,7 +327,7 @@ DataAccessObject.findById = function find(id, cb) { setIdValue(this, data, id); } obj = new this(); - obj._initProperties(data, true); + obj._initProperties(data, false); } cb(err, obj); }.bind(this)); @@ -426,7 +426,7 @@ DataAccessObject.find = function find(params, cb) { data.forEach(function (d, i) { var obj = new constr(); - obj._initProperties(d, true, params.fields); + obj._initProperties(d, false, params.fields); if (params && params.include && params.collect) { data[i] = obj.__cachedRelations[params.collect]; @@ -620,7 +620,7 @@ DataAccessObject.prototype.save = function (options, callback) { if (err) { return callback(err, inst); } - inst._initProperties(data, true); + inst._initProperties(data, false); updateDone.call(inst, function () { saveDone.call(inst, function () { callback(err, inst); diff --git a/lib/model-builder.js b/lib/model-builder.js index bf6995c6..a449bebf 100644 --- a/lib/model-builder.js +++ b/lib/model-builder.js @@ -267,7 +267,9 @@ ModelBuilder.prototype.define = function defineClass(className, properties, sett return this.__dataWas && this.__dataWas[attr]; }); - Object.defineProperty(ModelClass.prototype, '_' + attr, { + // FIXME: [rfeng] Do we need to keep the raw data? + // Use $ as the prefix to avoid conflicts with properties such as _id + Object.defineProperty(ModelClass.prototype, '$' + attr, { get: function () { return this.__data && this.__data[attr]; }, diff --git a/lib/model.js b/lib/model.js index 3c228e39..9c7f48f3 100644 --- a/lib/model.js +++ b/lib/model.js @@ -100,7 +100,16 @@ ModelBaseClass.prototype._initProperties = function (data, applySetters) { if (applySetters === true) { Object.keys(data).forEach(function (attr) { - if((attr in properties) || (attr in ctor.relations) || strict === false) { + if((attr in properties) || (attr in ctor.relations)) { + self[attr] = self.__data[attr] || data[attr]; + } + }); + } + + // Set the unknown properties as properties to the object + if(strict === false) { + Object.keys(data).forEach(function (attr) { + if(!(attr in properties)) { self[attr] = self.__data[attr] || data[attr]; } });