I have a collection in backbone... in the initialize method I'm doing this.... I want to rerender a view when this collection has an item added to it.
initialize: function (models, options) {
this.bind('add', function () {
NS.discussionView.reRender();
});
}
This solution works perfectly however mycoworker says I'm totally going against what backbone was designed to do. Is there a better approach to do this or is this an ok practice? The questions a bit subjective, really I want to know how I should correctly be doing something like this. Thanks for any feedback or advice.
Responding to the answer... Well the object model is deeper. The discussionView has a discussionModel, which has a topics property which is a backbone collection of topic models. Each topic has a replies collection of reply models. When a reply is added to a topic, I need to render the discussionview again. How can I set that chain up correctly?
Simply reverse the dependency: view might and should depend on model, but not the other way around. This way a single model might be used by several views (simple, detailed, etc.) and you might add views without modifying the model.
model.bind('add', function () {
this.render();
});
Also prefer render()
method name to follow Backbone.js naming convention.