简体   繁体   English

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

[英]Backbone : view doesn't update (reset event)

I'm currently using BackboneJS to get data from my database with a backbone model. 我目前正在使用BackboneJS通过骨干模型从数据库中获取数据。

The problem is that my app is not firing my reset event and my data are not displayed on the screen even if my fetch is well executed. 问题是我的应用程序没有触发我的重置事件,即使我的提取执行得很好,我的数据也没有显示在屏幕上。 If I execute app.messages.toJSON() in my console, my data are returned as demanded. 如果我在控制台中执行app.messages.toJSON(),则会按需返回我的数据。

Do you have any idea about it ? 你有什么想法吗? Here is my code 这是我的代码

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;
    }

});

I'm breaking my teeth on it. 我很伤心。

Simon 西蒙

EDIT 编辑

After all this code I have 在所有这些代码之后

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(); 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. 至于问题本身,如果您使用的是Backbone 1.0,则要触发reset event就必须使用reset flag
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. 此外,如果您添加的代码确实第一个代码之后 ,则您创建Router 之前要获取数据,因此要创建视图。 Even if the fetching method is asynchronous, you're not controlling what you're doing here. 即使获取方法是异步的,您也无法控制此处的操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM