Add support for nesting objects with an array

This commit is contained in:
Raymond Feng 2013-07-12 18:59:43 -07:00
parent 4cd9ea49ec
commit 8e01e17bdb
5 changed files with 36 additions and 18 deletions

View File

@ -18,8 +18,11 @@ var User = modelBuilder.define('User', {
emails: [{
label: String,
email: String
}]
}],
friends: [String]
});
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'}]});
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'}],
friends: ['John', 'Mary']});
console.log(user.toObject());

View File

@ -46,7 +46,7 @@ function List(data, type, parent) {
}
data.forEach(function(item, i) {
data[i] = new Item(item, list);
data[i] = Item(item, list);
Object.defineProperty(list, data[i].id, {
writable: true,
enumerable: false,
@ -147,12 +147,20 @@ List.prototype.find = function(pattern, field) {
}
};
List.prototype.toObject = function() {
return this.items;
List.prototype.toObject = function(onlySchema) {
var items = [];
this.items.forEach(function(item) {
if(item.toObject) {
items.push(item.toObject(onlySchema));
} else {
items.push(item);
}
});
return items;
};
List.prototype.toJSON = function() {
return this.items;
return this.toObject(true);
};
List.prototype.toString = function() {
@ -199,6 +207,9 @@ List.prototype.map = function(cb) {
};
function ListItem(data, parent) {
if(!(this instanceof ListItem)) {
return new ListItem(data, parent);
}
if (typeof data === 'object') {
for (var i in data) this[i] = data[i];
} else {

View File

@ -172,9 +172,13 @@ ModelBaseClass.prototype.toObject = function (onlySchema) {
this.constructor.forEachProperty(function (attr) {
if (self[attr] instanceof List) {
data[attr] = self[attr].toObject();
data[attr] = self[attr].toObject(onlySchema);
} else if (self.__data.hasOwnProperty(attr)) {
data[attr] = self[attr];
if(self[attr] !== undefined && self[attr]!== null && self[attr].toObject) {
data[attr] = self[attr].toObject(onlySchema);
} else {
data[attr] = self[attr];
}
} else {
data[attr] = null;
}

View File

@ -135,7 +135,7 @@ describe('DataSource define model', function () {
};
var user = new User({name: 'Joe'});
console.log(user);
// console.log(user);
// setup relationships
User.hasMany(Post, {as: 'posts', foreignKey: 'userId'});
@ -146,15 +146,15 @@ describe('DataSource define model', function () {
var user2 = new User({name: 'Smith'});
user2.save(function (err) {
console.log(user2);
// console.log(user2);
var post = user2.posts.build({title: 'Hello world'});
post.save(function (err, data) {
console.log(err ? err : data);
// console.log(err ? err : data);
});
});
Post.findOne({where: {published: false}, order: 'date DESC'}, function (err, data) {
console.log(data);
// console.log(data);
});
User.create({name: 'Jeff'}, function (err, data) {
@ -162,13 +162,13 @@ describe('DataSource define model', function () {
console.log(err);
return;
}
console.log(data);
// console.log(data);
var post = data.posts.build({title: 'My Post'});
console.log(post);
// console.log(post);
});
User.create({name: 'Ray'}, function (err, data) {
console.log(data);
// console.log(data);
});
var Article = ds.define('Article', {title: String});
@ -179,7 +179,7 @@ describe('DataSource define model', function () {
article.tags.create({name: 'popular'}, function (err, data) {
Article.findOne(function (e, article) {
article.tags(function (e, tags) {
console.log(tags);
// console.log(tags);
});
});
});
@ -249,7 +249,7 @@ describe('Load models from json', function () {
models.should.have.property('Customer');
for (var s in models) {
var m = models[s];
console.log(m.modelName, new m());
// console.log(m.modelName, new m());
}
});
});

View File

@ -196,7 +196,7 @@ describe('relations', function() {
Article.create(function(e, article) {
article.tags.create({name: 'popular'}, function(e, t) {
t.should.be.an.instanceOf(Tag);
console.log(t);
// console.log(t);
ArticleTag.findOne(function(e, at) {
should.exist(at);
at.tagId.toString().should.equal(t.id.toString());