diff --git a/examples/nesting-schema.js b/examples/nesting-schema.js index 36bccf96..22121e5e 100644 --- a/examples/nesting-schema.js +++ b/examples/nesting-schema.js @@ -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()); diff --git a/lib/list.js b/lib/list.js index 4eebd96a..3aee1538 100644 --- a/lib/list.js +++ b/lib/list.js @@ -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) { diff --git a/lib/model-builder.js b/lib/model-builder.js index 98120755..4fd1009b 100644 --- a/lib/model-builder.js +++ b/lib/model-builder.js @@ -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); + } } } }, diff --git a/test/adl.test.js b/test/adl.test.js index 1c1f4c4c..3ba8913a 100644 --- a/test/adl.test.js +++ b/test/adl.test.js @@ -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); });