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.
问题是绑定到重置的渲染函数在ajax获取请求后不会触发。
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.
如果我绑定它添加它的工作原理。我尝试将所有绑定到一个调试函数,它说同步事件与每个项目的添加一起被调用。
2 个解决方案
#1
33
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. It's now the default updating mechanism after a fetch. If you'd like to continue using "reset", pass
{reset: true}
重命名Collection的“更新”以设置,与类似的model.set()并行,并与reset对比。它现在是获取后的默认更新机制。如果您想继续使用“重置”,请通过{reset:true}
So, to trigger a reset event, you now have to use
因此,要触发重置事件,您现在必须使用
this.collection.fetch({reset: true})
#2
0
in backbone 1.0, you have to trigger reset by hand:
在主干1.0中,你必须手动触发重置:
youColloection.fetch({reset: true});
#1
33
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. It's now the default updating mechanism after a fetch. If you'd like to continue using "reset", pass
{reset: true}
重命名Collection的“更新”以设置,与类似的model.set()并行,并与reset对比。它现在是获取后的默认更新机制。如果您想继续使用“重置”,请通过{reset:true}
So, to trigger a reset event, you now have to use
因此,要触发重置事件,您现在必须使用
this.collection.fetch({reset: true})
#2
0
in backbone 1.0, you have to trigger reset by hand:
在主干1.0中,你必须手动触发重置:
youColloection.fetch({reset: true});