当前位置: 动力学知识库 > 问答 > 编程问答 >

javascript - How Can I Reset This Backbone View on Collection Change?

问题描述:

How can I bind this view to collection change events so that it Resets when a new item has been added to the collection?

KAC.Views.ModuleMainNavigation = Backbone.View.extend(

{

tagName: "div",

id: "",

className: "",

template: JST['modules/main_navigation'],

initialize: function() {

_.bindAll(this);

},

events: {

},

render: function () {

this.$el.html(

this.template(

{

collection : this.collection

}

)

);

return this;

}

}

);

网友答案:

You have to listen for the change event.
Most of the time this is done in the initialize function.

You could either listen for all events (model change, collection reset, new model, model removed):

this.collection.on('change reset add remove', this.render, this);

Or only for the new model added event:

this.collection.on('add', this.render, this);

See also backbone.js collection events

分享给朋友:
您可能感兴趣的文章:
随机阅读: