骨干集合取不火重置() [英] backbone collection fetch doesn't fire reset()
问题描述
这是我的一个集合视图
var mssg = mssg || {};
mssg.MessagesView = Backbone.View.extend({
el: '#messages',
initialize: function() {
this.collection.fetch();
this.collection.bind('reset', this.render, this);
},
render : function() {
this.$el.html('');
this.collection.each(function( item ) {
this.renderMessage( item );
}, this );
return this;
},
renderMessage : function( item ) {
var messageView = new mssg.MessageView({
model : item
});
this.$el.append( messageView.render().el );
}
});
这是集
var mssg = mssg || {};
mssg.Messages = Backbone.Collection.extend({
model : mssg.Message,
url : 'messages'
});
这是它是如何初始化的:
and this is how it is initialized:
var mssg = mssg || {};
$(function() {
new mssg.MessagesView({
collection : new mssg.Messages()
});
});
问题是,渲染
功能势必重置
不火的AJAX后取的请求。
The problem is that the render
function bound to reset
doesn't fire after the ajax fetch request.
如果我把它绑定到添加
它的工作原理。
我试着结合所有
来一个debuggin功能,它说,同步
事件被称为旁边的添加
为每个项目。
If I bind it to add
it works.
I tried binding all
to a debuggin function and it says that the sync
event is called alongside the add
for every item.
推荐答案
如果您检查骨干更改日志,你会看到顺便取指在1.0办理了更改:
If you check backbone change log, you'll see that the way fetch is handled changed in 1.0:
改名集合的更新设置,为并行与同类
model.set(),并用复位对比度。它现在默认的更新
后取机制。如果您想继续使用复位,通
{重置:真正}
Renamed Collection's "update" to set, for parallelism with the similar model.set(), and contrast with reset. It's now the default updating mechanism after a fetch. If you'd like to continue using "reset", pass
{reset: true}
所以,触发复位事件,你现在必须用
So, to trigger a reset event, you now have to use
this.collection.fetch({reset: true})
这篇关于骨干集合取不火重置()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!