0
采集模型通常当收集(S)的设计图,我会绑定collection
到视图,并注册相关的事件的collection
这样的:渲染骨干
var Book = Backbone.Model.extend({});
var BookList = Backbone.Collection.extend({
model: Book,
url: "/books"
});
var BookListItemView = Backbone.View.extend({
mtemplate: _.template($('#tpl_book_item').html()),
render: function() {
this.$el = $(this.mtemplate(this.model.toJSON()));
return this;
}
});
var BookListView = Backbone.View.extend({
el: '#content',
initialize: function() {
this.listenTo(this.collection, 'add', this.render);
this.listenTo(this.collection, 'remove', this.render);
},
render: function() {
this.$el.empty();
this.collection.each(function (item) {
this.$el.append(new BookListItemView({model: item}).render().$el);
}, this);
return this;
}
});
Use:
var books = new BookList();
var bookListView = new BookListView({
collection: books
});
books.fetch();
它的工作如预期:渲染每本书都在模板中定义。但是,我发现页面中存在一个小问题。
我不确定这是否是由重新渲染视图造成的?如图所示,当books.fetch
完成时,它会将书籍添加到books
的集合中,对于每个book
项目,将触发一个add
事件,然后通过删除存在的项目并迭代集合来重新呈现页面。
这意味着一旦有10本书,将有1+2+3+4...+10
循环的BookListView
。
我我看来,一旦add
事件触发的,我不应该刷新整个名单,但只是增加一个新的视图到BookListView
,但如何对remove
事件,似乎骨干不提供任何内部方法来获得从模型来看,所以一旦模型被删除,我就无法得到相关的视图。
你如何处理这种诉讼?
'让模型自己的View处理自己的remove事件'+1,我从来没有想过,谢谢。 – hguser
谢谢。我没有完全测试上面的代码,你可能需要改变一些东西。 – Exinferis