[英]backbone collection fetch doesn't fire reset()
This is my view for a collection 这是我对集合的看法
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 );
}
});
this is the collection 这是集合
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()
});
});
The problem is that the render
function bound to reset
doesn't fire after the ajax fetch request. 问题是绑定到
reset
的render
函数在ajax获取请求后不会触发。
If I bind it to add
it works. 如果我绑定它
add
它的工作原理。 I tried binding all
to a debuggin function and it says that the sync
event is called alongside the add
for every item. 我尝试将
all
绑定到一个调试函数,它说sync
事件与每个项目的add
一起被调用。
If you check backbone change log , you'll see that the way fetch is handled changed in 1.0: 如果检查主干更改日志 ,您将看到在1.0中更改了fetch的处理方式:
Renamed Collection's "update" to set, for parallelism with the similar model.set(), and contrast with reset.
重命名Collection的“更新”以设置,与类似的model.set()并行,并与reset对比。 It's now the default updating mechanism after a fetch.
它现在是获取后的默认更新机制。 If you'd like to continue using "reset", pass
{reset: true}
如果您想继续使用“重置”,请通过
{reset: true}
So, to trigger a reset event, you now have to use 因此,要触发重置事件,您现在必须使用
this.collection.fetch({reset: true})
在主干1.0中,你必须手动触发重置:
youColloection.fetch({reset: true});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.