0

我目前正在使用 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;
4

1 回答 1

0

app.messages = new app.Message(); I guess this is only a mistake you made while writing the question.

As for the question itself, if you're using Backbone 1.0, you'll have to use the reset flag if you want a reset event fired.
Furthermore, if the piece of code you added really is after the first one, you're fetching your data before creating the Router, therefore the view. Even if the fetching method is asynchronous, you're not controlling what you're doing here.

于 2013-04-08T14:41:30.567 回答