A polyfill for the {{on}}
element modifier specified by
RFC #471 "{{on}}
modifier".
ember install ember-on-modifier
- Completely inert when running
ember-source
3.11 or higher - Tested against
ember-source
v2.12, v2.18, v3.4 in CI
import Component from '@ember/component';
import { action } from '@ember-decorators/object';
export default class BritneySpearsComponent extends Component {
@action
onClick(event: MouseEvent) {
console.log('I must confess, I still believe.');
}
}
The @action
decorator is used to bind the onClick
method to the
component instance.
This is essentially equivalent to:
didInsertElement() {
super.didInsertElement();
const button = this.element.querySelector('button');
button.addEventListener('click', this.onClick);
}
In addition to the above {{on}}
will properly tear down the event listener,
when the element is removed from the DOM. It will also re-register the event
listener, if any of the passed parameters change.
You can use the {{on}}
modifier multiple times on the same element, even for
the same event.
All named parameters will be passed through to
addEventListener
as the third parameter, the options hash.
This is essentially equivalent to:
didInsertElement() {
super.didInsertElement();
const div = this.element.querySelector('div');
div.addEventListener('scroll', this.onScroll, { passive: true });
}
To fire an event listener only once, you can pass the once
option:
clickOnlyTheFirstTime
will only be fired the first time the button is clicked.
clickEveryTime
is fired every time the button is clicked, including the first
time.
To listen for an event during the capture phase already, use the capture
option:
If true
, you promise to not call event.preventDefault()
. This allows the
browser to optimize the processing of this event and not block the UI thread.
This prevent scroll jank.
If you still call event.preventDefault()
, an assertion will be raised.
Internet Explorer 11 has a buggy and incomplete implementation of
addEventListener
: It does not accept an
options
parameter and sometimes even throws
a cryptic error when passing options.
This is why this addon ships a tiny ponyfill for addEventLisener
that is used internally to emulate the once
, capture
and passive
option.
This means that all currently known options
are
polyfilled, so that you can rely on them in your logic.
If you want to curry the function call / partially apply arguments, you can do
so using the {{fn}}
helper:
import Component from '@ember/component';
import { action } from '@ember-decorators/object';
interface User {
name: string;
}
export default class UserListComponent extends Component {
users: User[] = [{ name: 'Tom Dale' }, { name: 'Yehuda Katz' }];
@action
deleteUser(user: User, event: MouseEvent) {
event.preventDefault();
this.users.removeObject(user);
}
}
The old {{action}}
modifier used to allow easily
calling event.preventDefault()
like so:
You also could easily call event.stopPropagation()
to avoid bubbling like so:
You can still do this using ember-event-helpers
:
-
ember-on-helper
: A complimentary{{on}
template helper that accepts arbitrary event targets.Also ships with two convenience helpers for adding event listeners to
document
andwindow
: