骨干集合获取不会触发 reset() [英] 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'
});
这是它的初始化方式:
var mssg = mssg || {};
$(function() {
new mssg.MessagesView({
collection : new mssg.Messages()
});
});
问题是绑定到 reset
的 render
函数在 ajax 获取请求后没有触发.
The problem is that the render
function bound to reset
doesn't fire after the ajax fetch request.
如果我将它绑定到 add
就可以了.我尝试将 all
绑定到调试函数,它说 sync
事件与每个项目的 add
一起调用.
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 中处理 fetch 的方式发生了变化:
If you check backbone change log, you'll see that the way fetch is handled changed in 1.0:
重命名集合的更新"以设置,以便与类似的并行model.set(),与reset对比.现在是默认更新获取后的机制.如果您想继续使用重置",请通过{reset: true}
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})
这篇关于骨干集合获取不会触发 reset()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!