Skip to content

Commit

Permalink
Remove Regions from Application.
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesplease committed Feb 4, 2015
1 parent 9dac0d9 commit 9f517ae
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 779 deletions.
140 changes: 1 addition & 139 deletions docs/marionette.application.md
Expand Up @@ -20,14 +20,6 @@ var MyApp = new Backbone.Marionette.Application();
* [initialize](#initialize)
* [Application Events](#application-events)
* [Starting An Application](#starting-an-application)
* [Application Regions (deprecated)](#application-regions)
* [jQuery Selector](#jquery-selector)
* [Custom Region Class](#custom-region-class)
* [Custom Region Class And Selector](#custom-region-class-and-selector)
* [Region Options](#region-options)
* [Overriding the default RegionManager](#overriding-the-default-regionmanager)
* [Get Region By Name](#get-region-by-name)
* [Removing Regions](#removing-regions)
* [Application.getOption](#applicationgetoption)
* [Adding Initializers (deprecated)](#adding-initializers)
* [The Application Channel (deprecated)](#the-application-channel)
Expand Down Expand Up @@ -98,136 +90,6 @@ var options = {
MyApp.start(options);
```

## Application Regions

> Warning: deprecated
> This feature is deprecated. Instead of using the Application as the root
> of your view tree, you should use a Layout View. To scope your Layout View to the entire
> document, you could set its `el` to 'body'. This might look something like the following:
>
>
> var RootView = Marionette.LayoutView.extend({
> el: 'body'
> });
>
Application instances have an API that allow you to manage [Regions](./marionette.region.md).
These Regions are typically the means through which your views become attached to the `document`.

You can create Regions through the `addRegions` method by passing in an object
literal or a function that returns an object literal.

There are three syntax forms for adding a region to an application object.

### jQuery Selector

The first is to specify a jQuery selector as the value of the region
definition. This will create an instance of a Marionette.Region directly,
and assign it to the selector:

```js
MyApp.addRegions({
someRegion: "#some-div",
anotherRegion: "#another-div"
});
```

### Custom Region Class

The second is to specify a custom region class, where the region class has
already specified a selector:

```js
var MyCustomRegion = Marionette.Region.extend({
el: "#foo"
});

MyApp.addRegions(function() {
return {
someRegion: MyCustomRegion
};
});
```

### Custom Region Class And Selector

The third method is to specify a custom region class, and a jQuery selector
for this region instance, using an object literal:

```js
var MyCustomRegion = Marionette.Region.extend({});

MyApp.addRegions({

someRegion: {
selector: "#foo",
regionClass: MyCustomRegion
},

anotherRegion: {
selector: "#bar",
regionClass: MyCustomRegion
}

});
```

### Region Options

You can also specify regions per `Application` instance.

```js
new Marionette.Application({
regions: {
fooRegion: '#foo-region'
}
});
```

### Overriding the default `RegionManager`

If you need the `RegionManager`'s class chosen dynamically, specify `getRegionManager`:

```js
Marionette.Application.extend({
// ...

getRegionManager: function() {
// custom logic
return new MyRegionManager();
}
```
This can be useful if you want to attach `Application`'s regions to your own instance of `RegionManager`.
### Get Region By Name
A region can be retrieved by name, using the `getRegion` method:
```js
var app = new Marionette.Application();
app.addRegions({ r1: "#region1" });

var myRegion = app.getRegion('r1');
```
Regions are also attached directly to the Application instance, **but this is not recommended usage**.
### Removing Regions
Regions can also be removed with the `removeRegion` method, passing in
the name of the region to remove as a string value:
```js
MyApp.removeRegion('someRegion');
```
Removing a region will properly empty it before removing it from the
application object.
For more information on regions, see [the region documentation](./marionette.region.md) Also, the API that Applications use to
manage regions comes from the RegionManager Class, which is documented [over here](./marionette.regionmanager.md).
### Application.getOption
Retrieve an object's attribute either directly from the object, or from the object's this.options, with this.options taking precedence.

Expand Down Expand Up @@ -260,7 +122,7 @@ MyApp.addInitializer(function(options){
var myView = new MyView({
model: options.someModel
});
MyApp.mainRegion.show(myView);
MyApp.rootView.mainRegion.show(myView);
});

MyApp.addInitializer(function(options){
Expand Down
4 changes: 2 additions & 2 deletions docs/marionette.layoutview.md
Expand Up @@ -222,7 +222,7 @@ var layoutView = new Marionette.LayoutView({
});

// Lastly, show the LayoutView in the App's mainRegion
MyApp.getRegion('main').show(layoutView);
MyApp.rootView.getRegion('main').show(layoutView);
```
You can nest LayoutViews as deeply as you want. This provides for a well organized,
Expand All @@ -235,7 +235,7 @@ var layout1 = new Layout1();
var layout2 = new Layout2();
var layout3 = new Layout3();

MyApp.getRegion('main').show(layout1);
MyApp.rootView.getRegion('main').show(layout1);

layout1.showChildView('region1', layout2);
layout2.showChildView('region2', layout3);
Expand Down
91 changes: 1 addition & 90 deletions src/application.js
@@ -1,12 +1,9 @@
// Application
// -----------

// Contain and manage the composite application as a whole.
// Stores and starts up `Region` objects, includes an
// event aggregator as `app.vent`
// A container for a Marionette application.
Marionette.Application = Marionette.Object.extend({
constructor: function(options) {
this._initializeRegions(options);
this._initCallbacks = new Marionette.Callbacks();
this.submodules = {};
_.extend(this, options);
Expand Down Expand Up @@ -40,38 +37,6 @@ Marionette.Application = Marionette.Object.extend({
this.triggerMethod('start', options);
},

// Add regions to your app.
// Accepts a hash of named strings or Region objects
// addRegions({something: "#someRegion"})
// addRegions({something: Region.extend({el: "#someRegion"}) });
addRegions: function(regions) {
return this._regionManager.addRegions(regions);
},

// Empty all regions in the app, without removing them
emptyRegions: function() {
return this._regionManager.emptyRegions();
},

// Removes a region from your app, by name
// Accepts the regions name
// removeRegion('myRegion')
removeRegion: function(region) {
return this._regionManager.removeRegion(region);
},

// Provides alternative access to regions
// Accepts the region name
// getRegion('main')
getRegion: function(region) {
return this._regionManager.get(region);
},

// Get all the regions from the region manager
getRegions: function(){
return this._regionManager.getRegions();
},

// Create a module, attached to the application
module: function(moduleNames, moduleDefinition) {

Expand All @@ -85,60 +50,6 @@ Marionette.Application = Marionette.Object.extend({
return ModuleClass.create.apply(ModuleClass, args);
},

// Enable easy overriding of the default `RegionManager`
// for customized region interactions and business-specific
// view logic for better control over single regions.
getRegionManager: function() {
return new Marionette.RegionManager();
},

// Internal method to initialize the regions that have been defined in a
// `regions` attribute on the application instance
_initializeRegions: function(options) {
var regions = _.isFunction(this.regions) ? this.regions(options) : this.regions || {};

this._initRegionManager();

// Enable users to define `regions` in instance options.
var optionRegions = Marionette.getOption(options, 'regions');

// Enable region options to be a function
if (_.isFunction(optionRegions)) {
optionRegions = optionRegions.call(this, options);
}

// Overwrite current regions with those passed in options
_.extend(regions, optionRegions);

this.addRegions(regions);

return this;
},

// Internal method to set up the region manager
_initRegionManager: function() {
this._regionManager = this.getRegionManager();
this._regionManager._parent = this;

this.listenTo(this._regionManager, 'before:add:region', function() {
Marionette._triggerMethod(this, 'before:add:region', arguments);
});

this.listenTo(this._regionManager, 'add:region', function(name, region) {
this[name] = region;
Marionette._triggerMethod(this, 'add:region', arguments);
});

this.listenTo(this._regionManager, 'before:remove:region', function() {
Marionette._triggerMethod(this, 'before:remove:region', arguments);
});

this.listenTo(this._regionManager, 'remove:region', function(name) {
delete this[name];
Marionette._triggerMethod(this, 'remove:region', arguments);
});
},

// Internal method to setup the Wreqr.radio channel
_initChannel: function() {
this.channelName = _.result(this, 'channelName') || 'global';
Expand Down

0 comments on commit 9f517ae

Please sign in to comment.