Skip to content
Easily and safely use @ember/object/events via a template helper
JavaScript HTML
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
addon Initial implementation Oct 23, 2019
app
config
tests
vendor
.editorconfig
.ember-cli
.eslintignore
.eslintrc.js
.gitignore
.npmignore
.template-lintrc.js
.travis.yml
.watchmanconfig
CONTRIBUTING.md
LICENSE.md
README.md
ember-cli-build.js
index.js
package.json
testem.js
yarn.lock

README.md

ember-add-listener-helper

Easily and safely use @ember/object/events via a template helper

{{add-listener @publisher 'event' this.onEvent}}

Usage

The @ember/object/events methods require careful accounting to avoid leaking event listeners, and are especially tricky to use when the subscriber is a glimmer component and the publisher is an argument to that component. Using the {{add-listener}} helper makes it a no-brainer, as it will handle registering, un/re-registering when arguments change, and unregistering on un-render. All you have to do is:

{{add-listener @publisher 'event' this.onEvent}}
export default class MyComponent extends Component {
  @action
  this.onEvent(arg1, arg2) {
    // handle event
  }
}

@publisher can be an object with events triggered using sendEvent from @ember/objects/events or using .trigger() from the Ember.Evented mixin.

Isn't Ember's eventing deprecated?

There is an RFC to deprecate Ember's eventing. As long as it's not deprecated and removed, this helper eases its use. If/when Ember's eventing is removed, this helper may also be deprecated, or may switch its implementation to using some other recommended pub/sub eventing library.

Compatibility

  • Ember.js v3.4 or above
  • Ember CLI v2.13 or above
  • Node.js v8 or above

Installation

ember install ember-add-listener-helper

Contributing

See the Contributing guide for details.

License

This project is licensed under the MIT License.

You can’t perform that action at this time.