Skip to content

Instantly share code, notes, and snippets.

@pc-rgundlapalli
Created November 28, 2012 19:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pc-rgundlapalli/4163461 to your computer and use it in GitHub Desktop.
Save pc-rgundlapalli/4163461 to your computer and use it in GitHub Desktop.
Handling backbone views
// adding close method to all backbone views
Backbone.View.prototype.close = function(){
this.remove();
this.unbind();
if (this.onClose){
this.onClose();
}
}
myView.js
MyView = Backbone.View.extend({
initialize: function(){
this.model.bind("change", this.render, this);
},
render: function(){ ... },
onClose: function(){
this.model.unbind("change", this.render);
//remove any other bindings that were made in this view
}
});
router.js
function showModule(moduleID){
if(moduleID != this.activeModuleID){
$('.module').hide();
$(moduleID).show();
//remove activeModuleView from memory
if(this.activeModuleView) {
this.activeModuleView.close();
}
this.activeModuleID = moduleID;
return true;
} else {
return false;
}
}
function showMyView(){
if(this.showModule('#myView')){
this.activeModuleView = new myView();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment