Skip to content

Instantly share code, notes, and snippets.

@syul
Last active November 3, 2015 18:44
Show Gist options
  • Save syul/20b708d0c5da2db7140c to your computer and use it in GitHub Desktop.
Save syul/20b708d0c5da2db7140c to your computer and use it in GitHub Desktop.
The right way to create view
var ContentView = BaseView.extend({
tagName:'div',
className: 'content',
views: {
"root": PickView,
"heroes": HeroesView
},
onInitialize: function (params) {
//reference to the current view
this.currentView = undefined;
BaseView.prototype.onInitialize.call(this, params);
Backbone.on("change:page", this.changePage, this);
},
changePage: function(viewName){
// safe removing of current view before adding new
if(this.currentView)
this.currentView.remove();
//var view = new this.views[viewName]();
//this.$el.html(view.render().el);
// adding the new view
this.currentView = new this.views[viewName]();
this.$el.html(this.currentView.render().el);
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment