Skip to content

Instantly share code, notes, and snippets.

@mikecx
Created May 22, 2013 19:04
Show Gist options
  • Save mikecx/5630047 to your computer and use it in GitHub Desktop.
Save mikecx/5630047 to your computer and use it in GitHub Desktop.
Backbone Marionette Regions Mixin
// 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);
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment