简体   繁体   中英

Backbone : view doesn't update (reset event)

I'm currently using BackboneJS to get data from my database with a backbone model.

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.

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.
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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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