Skip to content

Commit

Permalink
Merge pull request #13 from simonihmig/stop-events
Browse files Browse the repository at this point in the history
Call preventDefault() when returning false from view handler
  • Loading branch information
rwjblue authored Jun 12, 2017
2 parents 05c2af5 + b7b7373 commit 8c10506
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 0 deletions.
2 changes: 2 additions & 0 deletions addon/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,8 @@ export default Ember.EventDispatcher.extend({
do {
if (viewRegistry[target.id]) {
if (viewHandler(target, event) === false) {
event.preventDefault();
event.stopPropagation();
break;
}
} else if (target.hasAttribute('data-ember-action')) {
Expand Down
45 changes: 45 additions & 0 deletions tests/integration/components/test-event-dispatching-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,3 +91,48 @@ test('events bubble up', function(assert) {
focus('input');
blur('input');
});

test('events are not stopped by default', function(assert) {
assert.expect(2);

this.on('submit', (e) => {
e.preventDefault();
assert.ok(true, 'submit was fired!');
});

this.register('component:submit-button', Component.extend({
tagName: 'button',
attributeBindings: ['type'],
type: 'submit',
click() {
assert.ok(true, 'button was clicked!');
}
}));

this.render(hbs`<form onsubmit={{action "submit"}}>{{submit-button}}</form>`);

click('button');
});

test('events are stopped when returning false from view handler', function(assert) {
assert.expect(1);

this.on('submit', (e) => {
e.preventDefault();
assert.notOk(true, 'submit should not be fired!');
});

this.register('component:submit-button', Component.extend({
tagName: 'button',
attributeBindings: ['type'],
type: 'submit',
click() {
assert.ok(true, 'button was clicked!');
return false;
}
}));

this.render(hbs`<form onsubmit={{action "submit"}}>{{submit-button}}</form>`);

click('button');
});

0 comments on commit 8c10506

Please sign in to comment.