Created
November 28, 2012 19:25
-
-
Save pc-rgundlapalli/4163461 to your computer and use it in GitHub Desktop.
Handling backbone views
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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