Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make a public detachView #3155

Merged
merged 6 commits into from
Sep 7, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions docs/marionette.region.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ managing regions throughout your application.
* [Showing a View](#showing-a-view)
* [Hiding a View](#hiding-a-view)
* [Preserving Existing Views](#preserving-existing-views)
* [Detaching Existing Views](#detaching-existing-views)
* [Checking whether a region is showing a view](#checking-whether-a-region-is-showing-a-view)
* [`reset` A Region](#reset-a-region)

Expand Down Expand Up @@ -219,6 +220,23 @@ mainRegion.empty({preventDestroy: true});
**NOTE** When using `preventDestroy: true` you must be careful to cleanup your
old views manually to prevent memory leaks.

### Detaching Existing Views
If you want to detach an existing view from a region, use `detachView`.

```javascript
var myView = new MyView();

var myOtherView = new MyView();

var childView = new MyChildView();

// render and display the view
myView.showChildView('main', childView);

// ... somewhere down the line
myOtherView.showChildView('main', myView.getRegion('main').detachView());
```

### Checking whether a region is showing a view

If you wish to check whether a region has a view, you can use the `hasView`
Expand Down
29 changes: 29 additions & 0 deletions docs/marionette.view.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ multiple views through the `regions` attribute.
* [Managing Sub-views](#managing-sub-views)
* [Showing a view](#showing-a-view)
* [Accessing a child view](#accessing-a-child-view)
* [Detaching a child view](#detaching-a-child-view)
* [Organizing your View](#organizing-your-view)
* [Events](#events)
* [onEvent Listeners](#onevent-listeners)
Expand Down Expand Up @@ -198,6 +199,34 @@ var MyView = Mn.View.extend({

If no view is available, `getChildView` returns `null`.

#### Detaching a child view
You can detach a child view from a region through `detachChildView(region)`

```javascript

var Mn = require('backbone.marionette');
var SubView = require('./subview');

var MyView = Mn.View.extend({
template: '#tpl-view-with-regions',

regions: {
firstRegion: '#first-region',
secondRegion: '#second-region'
},

onRender: function() {
this.showChildView('firstRegion', new SubView());
},

onMoveView: function() {
var view = this.detachChildView('firstRegion');
this.showChildView('secondRegion', view);
}
});
```
This is a proxy for [region.detachView()](./marionette.region.md#detaching-existing-views)

### Region availability
Any defined regions within a `View` will be available to the `View` or any
calling code immediately after instantiating the `View`. This allows a View to
Expand Down
4 changes: 4 additions & 0 deletions src/mixins/regions.js
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,10 @@ export default {
return region.show(view, ...args);
},

detachChildView(name) {
return this.getRegion(name).detachView();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

forgot to do a region exist check

  const region = this.getRegion(name);
  if (!region) {
    return;
  }
  return region.detachView(); 

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FWIW that check isn't being done in other places

},

getChildView(name) {
return this.getRegion(name).currentView;
}
Expand Down
13 changes: 13 additions & 0 deletions src/region.js
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,19 @@ const Region = MarionetteObject.extend({
}
},

detachView() {
const view = this.currentView;

if (!view) {
return;
}

delete this.currentView;
this._detachView(view);
delete view._parent;
return view;
},

_detachView(view) {
const shouldTriggerDetach = !!view._isAttached;
if (shouldTriggerDetach) {
Expand Down
14 changes: 14 additions & 0 deletions test/unit/region.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -449,6 +449,20 @@ describe('region', function() {
});
});

describe('and the view is detached', function() {
beforeEach(function() {
this.detachedView = this.region.detachView();
this.noDetachedView = this.region.detachView();
});

it('should return the childView it was given', function() {
expect(this.detachedView).to.equal(this.view);
});

it('should not return a childView if it was already detached', function() {
expect(this.noDetachedView).to.be.undefined;
});
});
});

describe('when showing nested views', function() {
Expand Down
15 changes: 15 additions & 0 deletions test/unit/view.child-views.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,21 @@ describe('layoutView', function() {
it('childViewEvents are triggered', function() {
expect(this.childEventsHandler).to.have.been.calledOnce;
});

describe('and the view is detached', function() {
beforeEach(function() {
this.detachedView = this.layoutView.detachChildView('regionOne');
this.noDetachedView = this.layoutView.detachChildView('regionOne');
});

it('should return the childView it was given', function() {
expect(this.detachedView).to.equal(this.childView);
});

it('should not return a childView if it was already detached', function() {
expect(this.noDetachedView).to.be.undefined;
});
});
});

describe('when showing a layoutView via a region', function() {
Expand Down