主干:视图不更新(重置事件)



我目前正在使用BackboneJS从我的数据库中获取数据与骨干模型。

问题是,我的应用程序没有触发我的重置事件,我的数据没有显示在屏幕上,即使我的取回执行得很好。如果我在控制台中执行app.messages.toJSON(),我的数据将按要求返回。

你有什么想法吗?这是我的代码

var app = window.app = {};
// APP HERE
// Model
app.Message = Backbone.Model.extend({
    url : 'messages'
});
app.message = new app.Message;
// Collection
app.Messages = Backbone.Collection.extend({
    model : app.Message,
    url : 'messages'
});
// Views
app.messages = new app.Message(); 
app.messages.fetch();
app.ListView = Backbone.View.extend({
    template : Handlebars.compile($('#template-list').html()),
    initialize : function(){
        _.bindAll(this, 'render');
        //this.collection.on('reset', this.render); 
    },
    render : function(){
        this.$el.html(this.template({
            collection : this.collection.toJSON()
        }));
        return this;
    }
});

我都快把牙咬碎了。

西蒙

编辑在所有这些代码之后,我得到了
app.Router = Backbone.Router.extend({
    routes: {
        '*path' : 'home'
    },
    home: function(){
        this.views = {};
        this.views.list = new app.ListView({
            collection : app.messages
        });
        $('#list-shell').empty().append(this.views.list.render().el);
    }
});
app.router = new app.Router();
Backbone.history.start({pushState : true});
Backbone.emulateJSON = true;

app.messages = new app.Message();我猜这只是你在写问题时犯的一个错误。

至于问题本身,如果你使用的是Backbone 1.0,如果你想发射reset event,你必须使用reset flag
此外,如果您添加的这段代码确实是第一个代码之后,那么您在创建Router(即视图)之前获取数据。即使取回方法是异步的,你也不能控制你在这里做什么。

最新更新