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

preventDefault #5

Closed
buschtoens opened this issue Mar 5, 2019 · 7 comments
Closed

preventDefault #5

buschtoens opened this issue Mar 5, 2019 · 7 comments
Labels
enhancement New feature or request

Comments

@buschtoens
Copy link
Collaborator

{{action}} supports a preventDefault option to automatically call event.preventDefault():

<a href="/" {{action this.someAction preventDefault=true}}>Click me</a>

We could either add the same option to {{on}}:

<a href="/" {{on "click" this.someAction preventDefault=true}}>Click me</a>

Or explore alternative solutions. For instance, we could create a prevent-default helper:

<a href="/" {{on "click" (prevent-default this.someAction)}}>Click me</a>
<a href="/" {{on "click" this.someAction}} {{on "click" (prevent-default)}}>Click me</a>

Currently all named parameters are passed through to addEventListener as event options. Adding a preventDefault would mean that we introduce special handling for some options.

@buschtoens
Copy link
Collaborator Author

Exemplary prevent-default implementation:

import { helper } from '@ember/component/helper';

export const preventDefault = ([handler]) => event => {
  event.preventDefault();
  if (handler) handler(event);
};

export default helper(preventDefault);

@rwjblue
Copy link
Member

rwjblue commented Mar 5, 2019

I'd vaguely prefer a wrapping helper over adding a hash argument. Either of these seem good to me:

<a href="/" {{on "click" (prevent-default this.someAction)}}>Click me</a>
<a href="/" {{on "click" this.someAction}} {{on "click" (prevent-default)}}>Click me</a>

@NullVoxPopuli
Copy link

what if someone always wants to prevent default?

<a href="/" {{pdon "click" this.someAction}}>Click me</a>

idk

@lifeart
Copy link

lifeart commented Mar 5, 2019

<a href="/" {{on-local "click" this.someAction}}>Click me</a>

-> local = preventDefault & stopPropagation

<a href="/" {{on "local:click" this.someAction}}>Click me</a>

@lifeart
Copy link

lifeart commented Mar 5, 2019

<a href="/" {{on "click" (dom-event this.someAction stop=true prevent=true)}} >Click me</a>

@buschtoens
Copy link
Collaborator Author

Personally I am also leaning more towards the wrapping helper syntax. I am just not 100 % sure, whether these helpers should be included in this addon. I could add a config so that they may be stripped from the build.

stopPropagation is another interesting aspect. With the wrapping helper syntax applying both would look like this:

<a
  href="/"
  {{on "click" (prevent-default (stop-propagation this.someAction))}}
>
  Click me
</a>
<a
  href="/"
  {{on "click" this.someAction}}
  {{on "click" (prevent-default)}}
  {{on "click" (stop-propagation)}}
>
  Click me
</a>
<a
  href="/"
  {{on "click" this.someAction}}
  {{on "click" (prevent-default (stop-propagation))}}
>
  Click me
</a>

Besides stopPropagation there's also stopImmediatePropagation, but it is seldom used. List of all Event methods.

Tangentially related: There's been some talk about how we might want to swap out the underlying event system in the future. If we also add stopPropagation, we should be able to emulate it with a different event system, for instance Ember's EventManager.

@buschtoens buschtoens added the enhancement New feature or request label Mar 5, 2019
buschtoens added a commit that referenced this issue Mar 5, 2019
* feat(prevent-default): add `prevent-default` template helper

Closes #5.

* docs(README): add `preventDefault`
@buschtoens
Copy link
Collaborator Author

buschtoens commented Mar 6, 2019

I've released (prevent-default) in v0.7.0. 🚀
Thanks everybody! 👍

stopPropagation is tracked in #9. Excluding helpers from the build is tracked in #10.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

4 participants