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 2, 2016
1 parent f7865d9 commit ab1278b
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 25 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';

// MixinOptions
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 @@ -70,6 +70,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 @@ -78,6 +80,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 @@ -235,6 +247,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
2 changes: 1 addition & 1 deletion test/unit/view.child-views.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ describe('layoutView', function() {
var ChildView = Marionette.View.extend({
template: false,
onRender: function() {
this.triggerMethod('content:rendered');
this.trigger('content:rendered');
}
});
this.childView = new ChildView();
Expand Down

0 comments on commit ab1278b

Please sign in to comment.