Skip to content

Instantly share code, notes, and snippets.

@mikecx
Created May 22, 2013 19:04

Revisions

  1. mikecx created this gist May 22, 2013.
    48 changes: 48 additions & 0 deletions regions_mixin.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,48 @@
    // Mix regions into Backbone Views that don't have them.
    var Mixins = {
    Regions: {}
    };

    Mixins.Regions = function (parent) {
    var initialize = parent.prototype.initialize,
    render = parent.prototype.render,
    close = parent.prototype.close,
    functions = ['addRegion', 'addRegions', 'removeRegion', '_buildRegions',
    '_initializeRegions', '_reInitializeRegions', '_initRegionManager'];

    parent.prototype.initialize = function (options) {
    for(var func in functions) {
    parent.prototype[functions[func]] = Backbone.Marionette.Layout.prototype[functions[func]];
    };

    initialize.apply(this, arguments);

    this._firstRender = true;
    this._initializeRegions(options);
    };

    parent.prototype.render = function () {
    render.apply(this, arguments);

    if (this._firstRender){
    // if this is the first render, don't do anything to
    // reset the regions
    this._firstRender = false;
    } else if (this.isClosed){
    // a previously closed layout means we need to
    // completely re-initialize the regions
    this._initializeRegions();
    } else {
    // If this is not the first render call, then we need to
    // re-initializing the `el` for each region
    this._reInitializeRegions();
    }
    };

    parent.prototype.close = function () {
    if (this.isClosed){ return; }
    this.regionManager.close();

    close.apply(this, arguments);
    }
    };