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, state: String,
zipCode: String, zipCode: String,
country: 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'}}); 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); console.log(user.toObject());

View File

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

View File

@ -255,10 +255,14 @@ ModelBuilder.prototype.define = function defineClass(className, properties, sett
} }
if (value === null || value === undefined) { if (value === null || value === undefined) {
this.__data[attr] = value; this.__data[attr] = value;
} else {
if(DataType === List) {
this.__data[attr] = DataType(value, properties[attr].type, this.__data);
} else { } else {
this.__data[attr] = DataType(value); this.__data[attr] = DataType(value);
} }
} }
}
}, },
configurable: true, configurable: true,
enumerable: true enumerable: true

View File

@ -28,7 +28,7 @@ describe('ModelBuilder define model', function () {
modelBuilder.definitions.should.be.a('object').and.have.property('User'); modelBuilder.definitions.should.be.a('object').and.have.property('User');
var user = new User({name: 'Joe', age: 20}); var user = new User({name: 'Joe', age: 20});
console.log(user); // console.log(user);
User.modelName.should.equal('User'); User.modelName.should.equal('User');
user.should.be.a('object').and.have.property('name', 'Joe'); user.should.be.a('object').and.have.property('name', 'Joe');
@ -55,7 +55,11 @@ describe('ModelBuilder define model', function () {
state: String, state: String,
zipCode: String, zipCode: String,
country: String country: String
} },
emails: [{
label: String,
email: String
}]
}); });
// define any custom method // 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.models.should.be.a('object').and.have.property('User', User);
modelBuilder.definitions.should.be.a('object').and.have.property('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'}}); var user = new User({
console.log(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.modelName.should.equal('User');
user.should.be.a('object').and.have.property('name', 'Joe'); 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.should.have.property('address');
user.address.should.have.property('city', 'San Jose'); user.address.should.have.property('city', 'San Jose');
user.address.should.have.property('state', 'CA'); 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); done(null, User);
}); });