2 var _ = require('lodash');
3 var Marionette = require('backbone.marionette');
4 var behaviourLookup = require('./lookup');
6 var Behavior = Marionette.Behavior.extend({
11 onBeforeShow: function() {
15 setupRegions: function() {
16 /* Only perform setup once */
17 if (this.setup) return;
21 var isoRegions = this.options;
23 Object.keys(isoRegions).forEach(function(regionName) {
24 var isoRegionDefn = isoRegions[regionName];
25 var el = isoRegionDefn.el;
26 var region = view.addRegion(regionName, el);
27 var initMethod = isoRegionDefn.init;
29 if (!initMethod) return;
31 function optionsForRegion(options, config) {
32 var regionEl = region.$el[0];
33 if (!regionEl) throw new Error('Region ' + regionName + ' does not exist.');
35 var regionElChildLen = regionEl.children.length;
38 if (regionElChildLen === 0) {
40 } else if (regionElChildLen === 1) {
41 if (config && config.rerender) {
42 baseOptions = { el: regionEl.children[0] };
44 baseOptions = { template: false, el: regionEl.children[0] };
48 'Region can have zero or one elements, but not more. Region ' +
52 '. Are you sure you wrapped the region with a parent?'
56 if (!options) return baseOptions;
57 return _.extend(baseOptions, options);
60 // Allow the init methods to be specified as a string
61 if (typeof initMethod === 'string') {
62 initMethod = view[initMethod];
65 var childView = initMethod.call(view, optionsForRegion, region);
67 view.showChildView(regionName, childView);
73 behaviourLookup.register('Isomorphic', Behavior);
75 module.exports = Behavior;