Skip to content

Commit

Permalink
Merge pull request #17876 from simonihmig/fix-multiple-action-helpers
Browse files Browse the repository at this point in the history
[BUGFIX beta] Fix no-jQuery EventDispatcher to allow handling multiple element-space actions
  • Loading branch information
chancancode authored Apr 7, 2019
2 parents fb416b0 + 52b6657 commit c6d6c49
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -712,6 +712,45 @@ moduleFor(
this.assert.equal(originalHandlerWasCalled, true, 'the click handler was called');
}

['@test multiple actions with bubbles=false for same event are called but prevent bubbling']() {
let clickAction1WasCalled = false;
let clickAction2WasCalled = false;
let eventHandlerWasCalled = false;

let ExampleComponent = Component.extend({
actions: {
clicked1() {
clickAction1WasCalled = true;
},
clicked2() {
clickAction2WasCalled = true;
},
},
});

this.registerComponent('example-component', {
ComponentClass: ExampleComponent,
template: strip`
<a href="#"
{{action "clicked1" on="click" bubbles=false}}
{{action "clicked2" on="click" bubbles=false}}
>click me</a>`,
click() {
eventHandlerWasCalled = true;
},
});

this.render('{{example-component}}');

runTask(() => {
this.$('a').trigger('click');
});

this.assert.ok(clickAction1WasCalled, 'the first clicked action was called');
this.assert.ok(clickAction2WasCalled, 'the second clicked action was called');
this.assert.notOk(eventHandlerWasCalled, 'event did not bubble up');
}

['@test it should work properly in an #each block']() {
let editHandlerWasCalled = false;

Expand Down Expand Up @@ -1359,6 +1398,40 @@ moduleFor(
this.assert.ok(doubleClickActionWasCalled, 'the doubleClicked action was called');
}

['@test allows multiple actions for same event on a single element']() {
let clickAction1WasCalled = false;
let clickAction2WasCalled = false;

let ExampleComponent = Component.extend({
actions: {
clicked1() {
clickAction1WasCalled = true;
},
clicked2() {
clickAction2WasCalled = true;
},
},
});

this.registerComponent('example-component', {
ComponentClass: ExampleComponent,
template: strip`
<a href="#"
{{action "clicked1" on="click"}}
{{action "clicked2" on="click"}}
>click me</a>`,
});

this.render('{{example-component}}');

runTask(() => {
this.$('a').trigger('click');
});

this.assert.ok(clickAction1WasCalled, 'the first clicked action was called');
this.assert.ok(clickAction2WasCalled, 'the second clicked action was called');
}

['@test it should respect preventDefault option if provided']() {
let ExampleComponent = Component.extend({
actions: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -285,13 +285,16 @@ export default EmberObject.extend({
return;
}

let result = true;
for (let index = 0; index < actions.length; index++) {
let action = actions[index];

if (action && action.eventName === eventName) {
return action.handler(event);
// return false if any of the action handlers returns false
result = action.handler(event) && result;
}
}
return result;
};

// Special handling of events that don't bubble (event delegation does not work).
Expand Down

0 comments on commit c6d6c49

Please sign in to comment.