Does anyone have any thoughts on why this darn view won’t update when performing a .set on the model? I think if might have to do with the render functionality I’ve put in…
Backbone View:
App.Views.PanelOne = Backbone.View.extend({
initialize: function() {
console.log("View PanelOne Initialized");
panelonesetting = this.model.get('panel_one');
console.log( panelonesetting );
_.bindAll(this, 'render');
this.model.bind('change', this.render);
},
render: function(){
if ( panelonesetting == "poolprice" ){
this.$el.html(JST['dashboard/poolprice']({}));
return this;
};
if ( panelonesetting == "none" ){
this.$el.html(JST['dashboard/none']({}));
return this;
};
if ( panelonesetting == "noconnection" ){
this.$el.html(JST['dashboard/noconnection']({}));
return this;
};
}
});
Backbone Router:
App.Routers.Dashboard = Backbone.Router.extend({
routes: {
"" : "index"
},
initialize: function(options) {
console.log("Router initialized");
preflist = new App.Models.PrefList({});
preflist.fetch({
success: function() {
console.log( preflist );
paneloneview = new App.Views.PanelOne({ model: preflist });
$('#panelone').html(paneloneview.render().$el);
}
});
},
index: function() {
}
});
You have some accidental globals. In particular, you have this in your view’s
initialize:and then the
renderlooks atpanelonesetting. There is novaron thatpanelonesettingso it is a global variable.The
initializemethod is called when you construct your view and it is never called again; that means thatpanelonesettingwill be assigned the initialpanel_onevalue and then it is never updated. So, if the model is changed, yourrenderwill be called but it will be looking at the originalpanel_onesetting. The result is that nothing seems to change: you can call yourrenderas many times as you want with whatever model changes you want and you’ll never see anything happen.You should be checking the value of
panel_oneinside yourrendermethod:Your
preflist andpaneloneview` variables in your router are also global when they should almost certainly be local.