From c2eb1f55f07bb774ed470126b591aeee85868f7d Mon Sep 17 00:00:00 2001 From: Raymond Feng Date: Wed, 28 Aug 2013 21:49:05 -0700 Subject: [PATCH] Fix the property population for schemaless models --- lib/dao.js | 10 +++++----- lib/model.js | 20 +++++++++----------- test/loopback-dl.test.js | 11 +++++++++-- 3 files changed, 23 insertions(+), 18 deletions(-) diff --git a/lib/dao.js b/lib/dao.js index 3191bc19..c018fe57 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); + inst._initProperties(data, true); 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, false); + obj._initProperties(data, true); } cb(err, obj); }.bind(this)); @@ -424,9 +424,9 @@ DataAccessObject.find = function find(params, cb) { this.dataSource.connector.all(this.modelName, params, function (err, data) { if (data && data.forEach) { data.forEach(function (d, i) { - var obj = new constr; + var obj = new constr(); - obj._initProperties(d, false, params.fields); + obj._initProperties(d, true, 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, false); + inst._initProperties(data, true); updateDone.call(inst, function () { saveDone.call(inst, function () { callback(err, inst); diff --git a/lib/model.js b/lib/model.js index 77f13e1c..3c228e39 100644 --- a/lib/model.js +++ b/lib/model.js @@ -197,17 +197,15 @@ ModelBaseClass.toString = function () { */ ModelBaseClass.prototype.toObject = function (onlySchema) { var data = {}; - var ds = this.constructor.dataSource.definitions[this.constructor.modelName]; - var properties = ds.properties; var self = this; - var schemaless = this.constructor.settings.strict === false || !onlySchema; + var schemaLess = this.constructor.settings.strict === false || !onlySchema; this.constructor.forEachProperty(function (attr) { if (self[attr] instanceof List) { - data[attr] = self[attr].toObject(!schemaless); + data[attr] = self[attr].toObject(!schemaLess); } else if (self.__data.hasOwnProperty(attr)) { if(self[attr] !== undefined && self[attr]!== null && self[attr].toObject) { - data[attr] = self[attr].toObject(!schemaless); + data[attr] = self[attr].toObject(!schemaLess); } else { data[attr] = self[attr]; } @@ -216,18 +214,18 @@ ModelBaseClass.prototype.toObject = function (onlySchema) { } }); - if (schemaless) { - Object.keys(self).forEach(function (attr) { + if (schemaLess) { + Object.keys(self.__data).forEach(function (attr) { if (!data.hasOwnProperty(attr)) { - if(self[attr] !== undefined && self[attr]!== null && self[attr].toObject) { - data[attr] = self[attr].toObject(!schemaless); + var val = self.__data[attr]; + if(val !== undefined && val!== null && val.toObject) { + data[attr] = val.toObject(!schemaLess); } else { - data[attr] = self[attr]; + data[attr] = val; } } }); } - return data; }; diff --git a/test/loopback-dl.test.js b/test/loopback-dl.test.js index 9292fa06..53d91a74 100644 --- a/test/loopback-dl.test.js +++ b/test/loopback-dl.test.js @@ -301,15 +301,22 @@ describe('DataSource define model', function () { var ds = new DataSource('memory'); var User = ds.define('User', {}, {strict: false}); + User.modelName.should.equal('User'); User.create({name: 'Joe', age: 20}, function (err, user) { - User.modelName.should.equal('User'); user.should.be.a('object').and.have.property('name', 'Joe'); user.should.have.property('name', 'Joe'); user.should.have.property('age', 20); user.should.not.have.property('bio'); - done(null, User); + + User.findById(user.id, function(err, user) { + user.should.be.a('object').and.have.property('name', 'Joe'); + user.should.have.property('name', 'Joe'); + user.should.have.property('age', 20); + user.should.not.have.property('bio'); + done(null, User); + }); }); });