Skip to content

Commit

Permalink
Add auto addRegions behavior to regionMananger
Browse files Browse the repository at this point in the history
fixes #1902
  • Loading branch information
samccone committed Sep 26, 2014
1 parent b1f990a commit 89040c4
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 18 deletions.
37 changes: 19 additions & 18 deletions api/region-manager.jsdoc
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,24 @@ description: |

Even if you're not using this class redirectly, it is already being used by Marionette.Application and Marionette.LayoutView.

constructor: |
RegionManagers can be instantiated directly and can have regions added and removed via several methods.

```js
var rm = new Marionette.RegionManager();

var region = rm.addRegion("foo", "#bar");

var regions = rm.addRegions({
baz: "#baz",
quux: "ul.quux"
});

regions.baz.show(myView);

rm.removeRegion("foo");
```
constructor:
description: |
RegionManagers can be instantiated directly and can optionally have default regions passed into the constructor.

examples:
-
name: Creating a RegionMananger with default regions
example: |

```js
var mananger = new Marionette.RegionManager({
regions: {
"aRegion": "#bar"
}
})

mananger.getRegion('aRegion').show(new MyView);
```

functions:
addRegion:
Expand Down Expand Up @@ -361,4 +362,4 @@ interators: |
- last
- without
- isEmpty
- pluck
- pluck
15 changes: 15 additions & 0 deletions docs/marionette.regionmanager.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ objects.
## Documentation Index

* [Basic Use](#basic-use)
* [Constucting](#constructing)
* [RegionManager.addRegion](#regionmanageraddregion)
* [RegionManager.addRegions](#regionmanageraddregions)
* [addRegions default options](#addregions-default-options)
Expand Down Expand Up @@ -50,6 +51,20 @@ regions.baz.show(myView);
rm.removeRegion("foo");
```

## Constructing

The RegionMananger take an optional `region` option in their constructor. the regions are passed directly into `addRegions` for the region mananger instance.

```js
var mananger = new Marionette.RegionManager({
regions: {
"aRegion": "#bar"
}
});

mananger.getRegion('aRegion').show(new MyView);
```

## RegionManager.addRegion

Regions can be added individually using the `addRegion`
Expand Down
3 changes: 3 additions & 0 deletions src/region-manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ Marionette.RegionManager = (function(Marionette) {
var RegionManager = Marionette.Controller.extend({
constructor: function(options) {
this._regions = {};

Marionette.Controller.call(this, options);

this.addRegions(this.getOption('regions'));
},

// Add multiple regions using an object literal or a
Expand Down
28 changes: 28 additions & 0 deletions test/unit/region-manager.spec.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,34 @@
describe('regionManager', function() {
'use strict';

describe('instantiating a regionManager', function() {
beforeEach(function() {
this.context = $('<div><div id="thor"></div><div id="eos"></div></div>');
this.parentElHandler = this.sinon.stub().returns(this.context);

this.regions = {
"aRegion": "#thor",
"bRegion": "#eos"
};

this.addRegionSpy = this.sinon.stub();

this.RegionManager = Marionette.RegionManager.extend({
addRegions: this.addRegionSpy
});

this.regionManager = new this.RegionManager({
regions: this.regions
});
});

it('should pass regions to addRegions', function() {
expect(this.addRegionSpy).to.have
.been.calledWith(this.regions)
.and.to.have.been.calledOn(this.regionManager);
});
});

describe('.addRegion', function() {
describe('with a name and selector', function() {
beforeEach(function() {
Expand Down

0 comments on commit 89040c4

Please sign in to comment.