Fix the array data population

This commit is contained in:
Raymond Feng 2013-07-12 12:36:14 -07:00
parent 296b258f35
commit 09ab8356b8
4 changed files with 31 additions and 10 deletions

View File

@ -14,8 +14,12 @@ var User = modelBuilder.define('User', {
state: String,
zipCode: String,
country: String
}
},
emails: [{
label: String,
email: String
}]
});
var user = new User({name: 'Joe', age: 20, address: {street: '123 Main St', 'city': 'San Jose', state: 'CA'}});
console.log(user);
var user = new User({name: 'Joe', age: 20, address: {street: '123 Main St', 'city': 'San Jose', state: 'CA'}, emails: [{label: 'work', email: 'xyz@sample.com'}]});
console.log(user.toObject());

View File

@ -12,7 +12,7 @@ module.exports = List;
function List(data, type, parent) {
var list = this;
if (!(list instanceof List)) {
return new List(data);
return new List(data, type, parent);
}
if(typeof data === 'string') {
@ -42,7 +42,7 @@ function List(data, type, parent) {
var Item = list.ItemType = ListItem;
if (typeof type === 'object' && type.constructor.name === 'Array') {
list.ItemType = type[0] || ListItem;
Item = list.ItemType = type[0] || ListItem;
}
data.forEach(function(item, i) {

View File

@ -256,7 +256,11 @@ ModelBuilder.prototype.define = function defineClass(className, properties, sett
if (value === null || value === undefined) {
this.__data[attr] = value;
} else {
this.__data[attr] = DataType(value);
if(DataType === List) {
this.__data[attr] = DataType(value, properties[attr].type, this.__data);
} else {
this.__data[attr] = DataType(value);
}
}
}
},

View File

@ -28,7 +28,7 @@ describe('ModelBuilder define model', function () {
modelBuilder.definitions.should.be.a('object').and.have.property('User');
var user = new User({name: 'Joe', age: 20});
console.log(user);
// console.log(user);
User.modelName.should.equal('User');
user.should.be.a('object').and.have.property('name', 'Joe');
@ -55,7 +55,11 @@ describe('ModelBuilder define model', function () {
state: String,
zipCode: String,
country: String
}
},
emails: [{
label: String,
email: String
}]
});
// define any custom method
@ -66,8 +70,12 @@ describe('ModelBuilder define model', function () {
modelBuilder.models.should.be.a('object').and.have.property('User', User);
modelBuilder.definitions.should.be.a('object').and.have.property('User');
var user = new User({name: 'Joe', age: 20, address: {street: '123 Main St', 'city': 'San Jose', state: 'CA'}});
console.log(user);
var user = new User({
name: 'Joe', age: 20,
address: {street: '123 Main St', 'city': 'San Jose', state: 'CA'},
emails: [{label: 'work', email: 'xyz@sample.com'}]
});
// console.log(user);
User.modelName.should.equal('User');
user.should.be.a('object').and.have.property('name', 'Joe');
@ -77,6 +85,11 @@ describe('ModelBuilder define model', function () {
user.should.have.property('address');
user.address.should.have.property('city', 'San Jose');
user.address.should.have.property('state', 'CA');
user = user.toObject();
user.emails.should.have.property('length', 1);
user.emails[0].should.have.property('label', 'work');
user.emails[0].should.have.property('email', 'xyz@sample.com');
done(null, User);
});