From 97b1501c1fa05d31ad8a8cd633e3dda3ba0e4d4b Mon Sep 17 00:00:00 2001 From: Raymond Feng Date: Mon, 26 May 2014 08:21:23 -0700 Subject: [PATCH] Keep undefined/null values for the array type This allows connectors to distinguish between empty array and undefined/null. For example, mongodb will not override existing array properties if the value is undefined. --- lib/model.js | 11 +++++++---- test/loopback-dl.test.js | 9 ++++++--- 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/lib/model.js b/lib/model.js index b38b7120..508c5b6d 100644 --- a/lib/model.js +++ b/lib/model.js @@ -134,8 +134,9 @@ ModelBaseClass.prototype._initProperties = function (data, options) { } } + var propertyName; if (applySetters === true) { - for (var propertyName in data) { + for (propertyName in data) { if (typeof data[propertyName] !== 'function' && ((propertyName in properties) || (propertyName in ctor.relations))) { self[propertyName] = self.__data[propertyName] || data[propertyName]; } @@ -144,7 +145,7 @@ ModelBaseClass.prototype._initProperties = function (data, options) { // Set the unknown properties as properties to the object if (strict === false) { - for (var propertyName in data) { + for (propertyName in data) { if (typeof data[propertyName] !== 'function' && !(propertyName in properties)) { self[propertyName] = self.__data[propertyName] || data[propertyName]; } @@ -174,8 +175,10 @@ ModelBaseClass.prototype._initProperties = function (data, options) { } } if (type.name === 'Array' || Array.isArray(type)) { - if (!(self.__data[propertyName] instanceof List)) { - self.__data[propertyName] = new List(self.__data[propertyName], type, self); + if (!(self.__data[propertyName] instanceof List) + && self.__data[propertyName] !== undefined + && self.__data[propertyName] !== null ) { + self.__data[propertyName] = List(self.__data[propertyName], type, self); } } } diff --git a/test/loopback-dl.test.js b/test/loopback-dl.test.js index 6a1e013d..7f247007 100644 --- a/test/loopback-dl.test.js +++ b/test/loopback-dl.test.js @@ -582,7 +582,8 @@ describe('Models attached to a dataSource', function() { var ds = new DataSource('memory');// define models Post = ds.define('Post', { title: { type: String, length: 255, index: true }, - content: { type: String } + content: { type: String }, + comments: [String] }); }); @@ -613,9 +614,10 @@ describe('Models attached to a dataSource', function() { }); it('updateOrCreate should update the instance without removing existing properties', function (done) { - Post.create({title: 'a', content: 'AAA'}, function (err, post) { + Post.create({title: 'a', content: 'AAA', comments: ['Comment1']}, function (err, post) { post = post.toObject(); delete post.title; + delete post.comments; Post.updateOrCreate(post, function (err, p) { should.not.exist(err); p.id.should.be.equal(post.id); @@ -627,7 +629,8 @@ describe('Models attached to a dataSource', function() { should.not.exist(p._id); p.content.should.be.equal(post.content); p.title.should.be.equal('a'); - + p.comments.length.should.be.equal(1); + p.comments[0].should.be.equal('Comment1'); done(); }); });