Many of my Backbone models often deal with nested models and collections, so far I\'m using a combination of defaults
, parse
and toJSON
ma
I'v found out that with this approach Supplier's toJSON function will get outdated, so it might be a good idea to do reassemble back it's JSON state from it's, and it's children's data.
ACME.Supplier = Backbone.Model.extend({
initialize: function(options) {
this.tags = new ACME.Tags(options.tags);
},
parse: function(res) {
res.tags && this.tags.reset(res.tags);
return res;
},
toJSON: function({
return _.extend(
_.pick(this.attributes, 'id', 'attr1', 'attr2'), {
tags: this.tags.toJSON(),
});
})
});