Created
February 21, 2013 14:28
-
-
Save koenpunt/5005063 to your computer and use it in GitHub Desktop.
Very asic view manager for Backbone.js
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
/** | |
* | |
* Basic view manager | |
* | |
* var viewManager = new ViewManager({ | |
* container: '#container' | |
* }); | |
* | |
**/ | |
// Extend Backbone.View with a close function, which removes the | |
// view from the DOM and unbinds the events | |
_.extend(Backbone.View.prototype, { | |
close: function(){ | |
this.remove(); | |
this.unbind(); | |
} | |
}); | |
(function(){ | |
var ViewManager = (function(){ | |
function ViewManager(options){ | |
this.currentView = null; | |
this.$container = $(options.container); | |
}; | |
ViewManager.prototype = { | |
showView: function(view){ | |
if(this.currentView){ | |
this.currentView.close(); | |
} | |
this.currentView = view; | |
this.currentView.render(); | |
this.$container.html(this.currentView.el); | |
} | |
}; | |
return ViewManager; | |
})(); | |
this.ViewManager = ViewManager; | |
}).call(this); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment