{"id":15060325,"url":"https://github.com/buschtoens/ember-on-helper","last_synced_at":"2025-04-10T05:50:43.636Z","repository":{"id":47454363,"uuid":"186121778","full_name":"buschtoens/ember-on-helper","owner":"buschtoens","description":"Complimentary `{{on}}` template helper for listening to events on `document`, `window` or other event targets","archived":false,"fork":false,"pushed_at":"2023-12-15T17:29:20.000Z","size":657,"stargazers_count":12,"open_issues_count":25,"forks_count":1,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-03-24T07:04:11.319Z","etag":null,"topics":["ember","ember-addon","ember-helper","ember-template-helper","emberjs"],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/buschtoens.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2019-05-11T10:58:21.000Z","updated_at":"2024-05-30T03:11:21.000Z","dependencies_parsed_at":"2024-06-18T18:32:45.507Z","dependency_job_id":"e5114c90-6c19-4b62-bd82-58f717688838","html_url":"https://github.com/buschtoens/ember-on-helper","commit_stats":{"total_commits":202,"total_committers":8,"mean_commits":25.25,"dds":0.4257425742574258,"last_synced_commit":"d8a22c3b1304538dfed65acc8aaabf79a27deb8d"},"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/buschtoens%2Fember-on-helper","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/buschtoens%2Fember-on-helper/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/buschtoens%2Fember-on-helper/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/buschtoens%2Fember-on-helper/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/buschtoens","download_url":"https://codeload.github.com/buschtoens/ember-on-helper/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248101987,"owners_count":21048080,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["ember","ember-addon","ember-helper","ember-template-helper","emberjs"],"created_at":"2024-09-24T22:56:48.837Z","updated_at":"2025-04-10T05:50:43.592Z","avatar_url":"https://github.com/buschtoens.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# ember-on-helper\n\n[![Build Status](https://travis-ci.org/buschtoens/ember-on-helper.svg)](https://travis-ci.org/buschtoens/ember-on-helper)\n[![npm version](https://badge.fury.io/js/ember-on-helper.svg)](http://badge.fury.io/js/ember-on-helper)\n[![Download Total](https://img.shields.io/npm/dt/ember-on-helper.svg)](http://badge.fury.io/js/ember-on-helper)\n[![Ember Observer Score](https://emberobserver.com/badges/ember-on-helper.svg)](https://emberobserver.com/addons/ember-on-helper)\n[![Ember Versions](https://img.shields.io/badge/Ember.js%20Versions-%5E2.18%20%7C%7C%20%5E3.0-brightgreen.svg)](https://travis-ci.org/buschtoens/ember-on-helper)\n[![ember-cli Versions](https://img.shields.io/badge/ember--cli%20Versions-%5E2.13%20%7C%7C%20%5E3.0-brightgreen.svg)](https://travis-ci.org/buschtoens/ember-on-helper)\n[![code style: prettier](https://img.shields.io/badge/code_style-prettier-ff69b4.svg)](https://github.com/prettier/prettier)\n[![dependencies](https://img.shields.io/david/buschtoens/ember-on-helper.svg)](https://david-dm.org/buschtoens/ember-on-helper)\n[![devDependencies](https://img.shields.io/david/dev/buschtoens/ember-on-helper.svg)](https://david-dm.org/buschtoens/ember-on-helper)\n\nAn `{{on}}` template helper complimentary to the\n[RFC #471 \"`{{on}}` modifier\"](https://github.com/emberjs/rfcs/blob/master/text/0471-on-modifier.md).\n\n## Installation\n\n```\nember install ember-on-helper\n```\n\n#### Compatibility\n\n- Ember.js v2.18 or above\n- ember-cli v2.13 or above\n\n## But why?\n\nYou would use the `{{on}}` _modifier_ to register event listeners on elements\nthat are in the realm of your current template. But sometimes you need to\nregister event listeners on elements or even on generic `EventTarget`s that are\noutside of the control of your template, e.g. `document` or `window`.\n\n\u003e ⚠️👉 **WARNING:** Do not overuse this helper. If you want to bind to an\n\u003e element that _is_ controlled by Glimmer, but maybe just not by the current\n\u003e template, _do not_ reach for a manual `document.querySelector()`. Instead,\n\u003e think about your current template and state setup and try to use a true \"Data\n\u003e Down, Actions Up\" pattern or use a shared `Service` as a message bus.\n\n## Usage\n\nPretty much exactly the same as the `{{on}}` modifier, except for that the\n`{{on}}` helper expects one more positional parameter upfront: the `evenTarget`.\n\n```hbs\n{{on eventTarget eventName eventListener}}\n```\n\nAs with the `{{on}}` modifier, you can also pass optional event options as named\nparameters:\n\n```hbs\n{{on eventTarget eventName eventListener capture=bool once=bool passive=bool}}\n```\n\n### Simple Example\n\n```hbs\nClick anywhere in the browser window, fam.\n\n{{on this.document \"click\" this.onDocumentClick}}\n```\n\n```ts\nimport Component from '@glimmer/component';\nimport { action } from '@ember/object';\n\nexport default class TomstersWitnessComponent extends Component {\n  document = document;\n\n  @action\n  onDocumentClick(event: MouseEvent) {\n    console.log(\n      'Do you have a minute to talk about our Lord and Savior, Ember.js?'\n    );\n  }\n}\n```\n\nThis is essentially equivalent to:\n\n```ts\ndidInsertElement() {\n  super.didInsertElement();\n\n  document.addEventListener('click', this.onDocumentClick);\n}\n```\n\nIn addition to the above `{{on}}` will properly tear down the event listener,\nwhen the helper is removed from the DOM. It will also re-register the event\nlistener, if any of the passed parameters change.\n\nThe [`@action` decorator][@action] is used to bind the `onDocumentClick` method\nto the component instance. This is not strictly required here, since we do not\naccess `this`, but in order to not break with established patterns, we do it\nanyway.\n\n[@action]: https://github.com/emberjs/rfcs/blob/master/text/0408-decorators.md#method-binding\n\n### Listening to Events on `window` or `document`\n\nYou will often want to use the `{{on}}` helper to listen to events which are\nemitted on `window` or `document`. Because providing access to these globals in\nthe template as shown in **[Simple Example](#simple-example)** is quite\ncumbersome, `{{on}}` brings two friends to the party:\n\n- `{{on-document eventName eventListener}}`\n- `{{on-window eventName eventListener}}`\n\nThey work exactly the same way as `{{on}}` and also accept event options.\n\n### Listening to Multiple Events\n\nYou can use the `{{on}}` helper multiple times in the same template and for the\nsame event target, even for the same event.\n\n```hbs\n{{on this.someElement \"click\" this.onClick}}\n{{on this.someElement \"click\" this.anotherOnClick}}\n{{on this.someElement \"mousemove\" this.onMouseMove}}\n```\n\n### Event Options\n\nAll named parameters will be passed through to\n[`addEventListener`][addeventlistener] as the third parameter, the options hash.\n\n[addeventlistener]: https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/addEventListener\n\n```hbs\n{{on-document \"scroll\" this.onScroll passive=true}}\n```\n\nThis is essentially equivalent to:\n\n```ts\ndidInsertElement() {\n  super.didInsertElement();\n\n  document.addEventListener('scroll', this.onScroll, { passive: true });\n}\n```\n\n#### `once`\n\nTo fire an event listener only once, you can pass the [`once` option][addeventlistener-parameters]:\n\n```hbs\n{{on-window \"click\" this.clickOnlyTheFirstTime once=true}}\n{{on-window \"click\" this.clickEveryTime}}\n```\n\n`clickOnlyTheFirstTime` will only be fired the first time the page is clicked.\n`clickEveryTime` is fired every time the page is clicked, including the first\ntime.\n\n[addeventlistener-parameters]: https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/addEventListener#Parameters\n\n#### `capture`\n\nTo listen for an event during the capture phase already, use the [`capture` option][addeventlistener-parameters]:\n\n```hbs\n{{on-document \"click\" this.triggeredFirst capture=true}}\n\n\u003cbutton {{on \"click\" this.triggeredLast}}\u003e\n  Click me baby, one more time!\n\u003c/button\u003e\n```\n\n#### `passive`\n\nIf `true`, you promise to not call `event.preventDefault()`. This allows the\nbrowser to optimize the processing of this event and not block the UI thread.\nThis prevent scroll jank.\n\nIf you still call `event.preventDefault()`, an assertion will be raised.\n\n```hbs\n{{on-document \"scroll\" this.trackScrollPosition passive=true}}\u003e\n```\n\n#### Internet Explorer 11 Support\n\nInternet Explorer 11 has a buggy and incomplete implementation of\n`addEventListener`: It does not accept an\n[`options`][addeventlistener-parameters] parameter and _sometimes_ even throws\na cryptic error when passing options.\n\nThis is why this addon ships a tiny [ponyfill][ponyfill] for `addEventLisener`\nthat is used internally to emulate the `once`, `capture` and `passive` option.\nThis means that all currently known [`options`][addeventlistener-parameters] are\npolyfilled, so that you can rely on them in your logic.\n\n[ponyfill]: https://github.com/sindresorhus/ponyfill\n\n### Currying / Partial Application\n\nIf you want to curry the function call / partially apply arguments, you can do\nso using the [`{{fn}}` helper][fn-helper]:\n\n[fn-helper]: https://github.com/emberjs/rfcs/blob/master/text/0470-fn-helper.md\n\n```hbs\n{{#each this.videos as |video|}}\n  {{on video.element \"play\" (fn this.onPlay video)}}\n  {{on video.element \"pause\" (fn this.onPause video)}}\n{{/each}}\n```\n\n```ts\nimport Component from '@ember/component';\nimport { action } from '@ember-decorators/object';\n\ninterface Video {\n  element: HTMLVideoElement;\n  title: string;\n}\n\nexport default class UserListComponent extends Component {\n  videos: Video[];\n\n  @action\n  onPlay(video: Video, event: MouseEvent) {\n    console.log(`Started playing '${video.title}'.`);\n  }\n\n  @action\n  onPlay(video: Video, event: MouseEvent) {\n    console.log(`Paused '${video.title}'.`);\n  }\n}\n```\n\n### `preventDefault` / `stopPropagation` / `stopImmediatePropagation`\n\nThe old [`{{action}}` modifier][action-event-propagation] used to allow easily\ncalling `event.preventDefault()` like so:\n\n```hbs\n\u003ca href=\"/\" {{action this.someAction preventDefault=true}}\u003eClick me\u003c/a\u003e\n```\n\n[action-event-propagation]: https://www.emberjs.com/api/ember/release/classes/Ember.Templates.helpers/methods/action?anchor=action#event-propagation\n\nYou also could easily call `event.stopPropagation()` to avoid bubbling like so:\n\n```hbs\n\u003ca href=\"/\" {{action this.someAction bubbles=false}}\u003eClick me\u003c/a\u003e\n```\n\nYou can still do this using [`ember-event-helpers`][ember-event-helpers]:\n\n[ember-event-helpers]: https://github.com/buschtoens/ember-event-helpers\n\n```hbs\n\u003ca href=\"/\" {{on \"click\" (prevent-default this.someAction)}}\u003eClick me\u003c/a\u003e\n```\n\n```hbs\n\u003ca href=\"/\" {{on \"click\" (stop-propagation this.someAction)}}\u003eClick me\u003c/a\u003e\n```\n\n## Attribution\n\nThis addon is a straight copy of [`ember-on-modifier`][ember-on-modifier], the\npolyfill for the `{{on}}` modifier.\n\n[ember-on-modifier]: https://github.com/buschtoens/ember-on-modifier\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbuschtoens%2Fember-on-helper","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbuschtoens%2Fember-on-helper","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbuschtoens%2Fember-on-helper/lists"}