Skip to content

Commit

Permalink
View childViewEvents should support trigger
Browse files Browse the repository at this point in the history
Resolves #2667 and #2464
  • Loading branch information
paulfalgout committed Nov 10, 2016
1 parent 494d1a6 commit 2061f12
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 27 deletions.
24 changes: 0 additions & 24 deletions src/mixins/view.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import CommonMixin from './common';
import DelegateEntityEventsMixin from './delegate-entity-events';
import TriggersMixin from './triggers';
import UIMixin from './ui';
import View from '../view';
import MarionetteError from '../error';
import DomMixin from './dom';

Expand Down Expand Up @@ -189,7 +188,6 @@ const ViewMixin = {
const ret = triggerMethod.apply(this, arguments);

this._triggerEventOnBehaviors.apply(this, arguments);
this._triggerEventOnParentLayout.apply(this, arguments);

return ret;
},
Expand All @@ -200,28 +198,6 @@ const ViewMixin = {
this._childViewTriggers = _.result(this, 'childViewTriggers');
},

_triggerEventOnParentLayout() {
const layoutView = this._parentView();
if (!layoutView) {
return;
}

layoutView._childViewEventHandler.apply(layoutView, arguments);
},

// Walk the _parent tree until we find a view (if one exists).
// Returns the parent view hierarchically closest to this view.
_parentView() {
let parent = this._parent;

while (parent) {
if (parent instanceof View) {
return parent;
}
parent = parent._parent;
}
},

_childViewEventHandler(eventName, ...args) {
const childViewEvents = this.normalizeMethods(this._childViewEvents);

Expand Down
15 changes: 15 additions & 0 deletions src/region.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ const Region = MarionetteObject.extend({
// the child may trigger during render can also be triggered on the child's ancestor views.
view._parent = this;

this._proxyChildViewEvents(view);

this._renderView(view);

this._attachView(view, options);
Expand All @@ -79,6 +81,16 @@ const Region = MarionetteObject.extend({
return this;
},

_proxyChildViewEvents(view) {
const parentView = this._parent;

if (!parentView) {
return;
}

parentView.listenTo(view, 'all', parentView._childViewEventHandler);
},

_renderView(view) {
if (view._isRendered) {
return;
Expand Down Expand Up @@ -230,6 +242,9 @@ const Region = MarionetteObject.extend({
if (!view._isDestroyed) {
this._removeView(view, shouldDestroy);
delete view._parent;
if (this._parent) {
this._parent.stopListening(view);
}
}

this.triggerMethod('empty', this, view);
Expand Down
15 changes: 12 additions & 3 deletions test/unit/view.child-views.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -287,18 +287,23 @@ describe('layoutView', function() {
describe('when showing a childView as a View', function() {
beforeEach(function() {
this.layoutView = new this.View();
this.childEventsHandler = this.sinon.spy();
this.childEventsHandlerTrigger = this.sinon.spy();
this.childEventsHandlerTriggerMethod = this.sinon.spy();

// add child events to listen for
this.layoutView.childViewEvents = {
'content:rendered': this.childEventsHandler
'before:content:rendered': this.childEventsHandlerTrigger,
'content:rendered': this.childEventsHandlerTriggerMethod
};
this.layoutView.delegateEvents();
this.layoutView.render();

// create a child view which triggers an event on render
var ChildView = Marionette.View.extend({
template: false,
onBeforeRender: function() {
this.trigger('before:content:rendered');
},
onRender: function() {
this.triggerMethod('content:rendered');
}
Expand All @@ -313,7 +318,11 @@ describe('layoutView', function() {
});

it('childViewEvents are triggered', function() {
expect(this.childEventsHandler).to.have.been.calledOnce;
expect(this.childEventsHandlerTrigger).to.have.been.calledOnce;
});

it('childViewEvents are triggered', function() {
expect(this.childEventsHandlerTriggerMethod).to.have.been.calledOnce;
});

describe('and the view is detached', function() {
Expand Down

0 comments on commit 2061f12

Please sign in to comment.