Plato on Github
Report Home
regionManager.js
Maintainability
74.10
Lines of code
122
Difficulty
28.37
Estimated Errors
0.92
Function weight
By Complexity
By SLOC
// Marionette.RegionManager // ------------------------ // // Manage one or more related `Marionette.Region` objects. Marionette.RegionManager = (function(Marionette){ var RegionManager = Marionette.Controller.extend({ constructor: function(options){ this._regions = {}; Marionette.Controller.prototype.constructor.call(this, options); }, // Add multiple regions using an object literal, where // each key becomes the region name, and each value is // the region definition. addRegions: function(regionDefinitions, defaults){ var regions = {}; _.each(regionDefinitions, function(definition, name){ if (typeof definition === "string"){ definition = { selector: definition }; } if (definition.selector){ definition = _.defaults({}, definition, defaults); } var region = this.addRegion(name, definition); regions[name] = region; }, this); return regions; }, // Add an individual region to the region manager, // and return the region instance addRegion: function(name, definition){ var region; var isObject = _.isObject(definition); var isString = _.isString(definition); var hasSelector = !!definition.selector; if (isString || (isObject && hasSelector)){ region = Marionette.Region.buildRegion(definition, Marionette.Region); } else if (_.isFunction(definition)){ region = Marionette.Region.buildRegion(definition, Marionette.Region); } else { region = definition; } this._store(name, region); this.triggerMethod("region:add", name, region); return region; }, // Get a region by name get: function(name){ return this._regions[name]; }, // Remove a region by name removeRegion: function(name){ var region = this._regions[name]; this._remove(name, region); }, // Close all regions in the region manager, and // remove them removeRegions: function(){ _.each(this._regions, function(region, name){ this._remove(name, region); }, this); }, // Close all regions in the region manager, but // leave them attached closeRegions: function(){ _.each(this._regions, function(region, name){ region.close(); }, this); }, // Close all regions and shut down the region // manager entirely close: function(){ this.removeRegions(); var args = Array.prototype.slice.call(arguments); Marionette.Controller.prototype.close.apply(this, args); }, // internal method to store regions _store: function(name, region){ this._regions[name] = region; this.length = _.size(this._regions); }, // internal method to remove a region _remove: function(name, region){ region.close(); delete this._regions[name]; this.triggerMethod("region:remove", name, region); } }); // Borrowing this code from Backbone.Collection: // http://backbonejs.org/docs/backbone.html#section-106 // // Mix in methods from Underscore, for iteration, and other // collection related features. var methods = ['forEach', 'each', 'map', 'find', 'detect', 'filter', 'select', 'reject', 'every', 'all', 'some', 'any', 'include', 'contains', 'invoke', 'toArray', 'first', 'initial', 'rest', 'last', 'without', 'isEmpty', 'pluck']; _.each(methods, function(method) { RegionManager.prototype[method] = function() { var regions = _.values(this._regions); var args = [regions].concat(_.toArray(arguments)); return _[method].apply(_, args); }; }); return RegionManager; })(Marionette);