koenpunt
2/21/2013 - 2:28 PM

Very asic view manager for Backbone.js

Very asic view manager for Backbone.js

/** 
 *
 * 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);