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.
This commit is contained in:
Raymond Feng 2014-05-26 08:21:23 -07:00
parent 83979cad7a
commit 97b1501c1f
2 changed files with 13 additions and 7 deletions

View File

@ -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);
}
}
}

View File

@ -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();
});
});