Permalink
Browse files

v0.11.0 pre-release build

  • Loading branch information...
1 parent fefbff1 commit 1a6e42b785c4f38fdcf045cb6d62f0f03d9e5b37 @derickbailey derickbailey committed Oct 3, 2012
Showing with 180 additions and 94 deletions.
  1. +89 −46 lib/amd/backbone.marionette.js
  2. +1 −1 lib/amd/backbone.marionette.min.js
  3. +89 −46 lib/backbone.marionette.js
  4. +1 −1 lib/backbone.marionette.min.js
@@ -656,17 +656,25 @@
// Region
// ------
-
+ //
// Manage the visual regions of your composite application. See
// http://lostechies.com/derickbailey/2011/12/12/composite-js-apps-regions-and-region-managers/
+
Marionette.Region = function(options){
this.options = options || {};
+ var el = this.options.el;
+ delete this.options.el;
+
var eventBinder = new Marionette.EventBinder();
_.extend(this, eventBinder, options);
+ if (el){
+ this.el = el;
+ }
+
if (!this.el){
- var err = new Error("An 'el' must be specified");
+ var err = new Error("An 'el' must be specified for a region.");
err.name = "NoElError";
throw err;
}
@@ -676,6 +684,76 @@
}
};
+
+ // Region Type methods
+ // -------------------
+
+ _.extend(Marionette.Region, {
+
+ // Build an instance of a region by passing in a configuration object
+ // and a default region type to use if none is specified in the config.
+ //
+ // The config object should either be a string as a jQuery DOM selector,
+ // a Region type directly, or an object literal that specifies both
+ // a selector and regionType:
+ //
+ // ```js
+ // {
+ // selector: "#foo",
+ // regionType: MyCustomRegion
+ // }
+ // ```
+ //
+ buildRegion: function(regionConfig, defaultRegionType){
+ var regionIsString = (typeof regionConfig === "string");
+ var regionSelectorIsString = (typeof regionConfig.selector === "string");
+ var regionTypeIsUndefined = (typeof regionConfig.regionType === "undefined");
+ var regionIsType = (typeof regionConfig === "function");
+
+ if (!regionIsType && !regionIsString && !regionSelectorIsString) {
+ throw new Error("Region must be specified as a Region type, a selector string or an object with selector property");
+ }
+
+ var selector, RegionType;
+
+ // get the selector for the region
+
+ if (regionIsString) {
+ selector = regionConfig;
+ }
+
+ if (regionConfig.selector) {
+ selector = regionConfig.selector;
+ }
+
+ // get the type for the region
+
+ if (regionIsType){
+ RegionType = regionConfig;
+ }
+
+ if (!regionIsType && regionTypeIsUndefined) {
+ RegionType = defaultRegionType;
+ }
+
+ if (regionConfig.regionType) {
+ RegionType = regionConfig.regionType;
+ }
+
+ // build the region instance
+
+ var regionManager = new RegionType({
+ el: selector
+ });
+
+ return regionManager;
+ }
+
+ });
+
+ // Region Instance Methods
+ // -----------------------
+
_.extend(Marionette.Region.prototype, Backbone.Events, {
// Displays a backbone view instance inside of the region.
@@ -810,34 +888,11 @@
var that = this;
_.each(this.regions, function (region, name) {
- var regionIsString = (typeof region === "string");
- var regionSelectorIsString = (typeof region.selector === "string");
- var regionTypeIsUndefined = (typeof region.regionType === "undefined");
-
- if (!regionIsString && !regionSelectorIsString) {
- throw new Error("Region must be specified as a selector string or an object with selector property");
- }
- var selector, RegionType;
-
- if (regionIsString) {
- selector = region;
- } else {
- selector = region.selector;
- }
-
- if (regionTypeIsUndefined){
- RegionType = that.regionType;
- } else {
- RegionType = region.regionType;
- }
-
- var regionManager = new RegionType({
- el: selector,
- getEl: function(selector){
- return that.$(selector);
- }
- });
+ var regionManager = Marionette.Region.buildRegion(region, that.regionType);
+ regionManager.getEl = function(selector){
+ return that.$(selector);
+ };
that.regionManagers[name] = regionManager;
that[name] = regionManager;
@@ -918,23 +973,11 @@
// addRegions({something: "#someRegion"})
// addRegions{{something: Region.extend({el: "#someRegion"}) });
addRegions: function(regions){
- var RegionValue, regionObj, region;
-
- for(region in regions){
- if (regions.hasOwnProperty(region)){
- RegionValue = regions[region];
-
- if (typeof RegionValue === "string"){
- regionObj = new Marionette.Region({
- el: RegionValue
- });
- } else {
- regionObj = new RegionValue();
- }
-
- this[region] = regionObj;
- }
- }
+ var that = this;
+ _.each(regions, function (region, name) {
+ var regionManager = Marionette.Region.buildRegion(region, Marionette.Region);
+ that[name] = regionManager;
+ });
},
// Removes a region from your app.
Oops, something went wrong.

0 comments on commit 1a6e42b

Please sign in to comment.