javascript - How to remove nested collection when destroying model? -
i'm initializing nested collection te following:
var post = { id: 123, title: 'sterling archer', comments: [ {text: 'comment text', tags: ['tag1', 'tag2', 'tag3']}, {text: 'comment test', tags: ['tag2', 'tag5']} ] }; var postmodel = backbone.model.extend({ parse: function (response) { if (response.comments) { response.comments = new backbone.collection(response.comments); } return response; } }); var post = new postmodel(post, {parse: true});
how should remove nested 'comments' collection when removing model?
post.destroy();
you can override destroy
method of postmodel
instead of sync
(which not called in case of new model without id
attribute):
destroy: function(options) { this.get('comments').each(function(mdl) { mdl.destroy(); }); backbone.model.prototype.destroy.call(this, options) }
Comments
Post a Comment