Question

I've a navigation view which is rendering again if the session status has changed. I want to have the same if the object user changes in the session model. But the line has no effect as the user object is not a attribute defined in the default section. Is there a way to listen or bind the render method of the NavView to the changes of the user object of the session model?

window.NavView = Backbone.View.extend({

    template : _.template($('#tpl-nav').html()),

    initialize : function() {
        window.session.on("change:logged_in", this.render, this);
        // this one is not working      
        window.session.on("change:user", this.render, this);
    },
);

window.SessionModel = Backbone.Model.extend({

    defaults : {
        logged_in : false,
        user_id : ''
    },

    initialize : function() {
        this.user = new User({ });
    }
});

Thanks

Was it helpful?

Solution

Add this to your code SessionModel.initialize :

var self = this;
this.user = new User({ });
this.user.bind('change', function() { self.trigger('change:user'); });
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top