骨干集​​合取不火重置() [英] backbone collection fetch doesn't fire reset()

查看:90
本文介绍了骨干集​​合取不火重置()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的一个集合视图

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆