https://github.com/nullvoxpopuli/ember-lifecycle-utils
Utils for lifecycle-related things in Ember
https://github.com/nullvoxpopuli/ember-lifecycle-utils
Last synced: over 1 year ago
JSON representation
Utils for lifecycle-related things in Ember
- Host: GitHub
- URL: https://github.com/nullvoxpopuli/ember-lifecycle-utils
- Owner: NullVoxPopuli
- License: mit
- Created: 2021-05-05T21:55:29.000Z (about 5 years ago)
- Default Branch: main
- Last Pushed: 2025-04-13T23:16:15.000Z (over 1 year ago)
- Last Synced: 2025-04-15T17:00:04.475Z (over 1 year ago)
- Language: JavaScript
- Size: 1.2 MB
- Stars: 5
- Watchers: 2
- Forks: 1
- Open Issues: 11
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- Contributing: CONTRIBUTING.md
- License: LICENSE.md
Awesome Lists containing this project
README
# ember-lifecycle-utils
[](https://badge.fury.io/js/ember-lifecycle-utils)
[](https://github.com/NullVoxPopuli/ember-lifecycle-utils/actions/workflows/ci.yml)
Utils to reduce boilerplate when working with lifecycles
## Installation
```
ember install ember-lifecycle-utils
```
## Usage
### Vanilla Classes and Destroyables
```js
import { withCleanup } from 'ember-lifecycle-utils';
class Hello {
constructor() {
withCleanup(this, () => {
window.addEventListener('click', this.handleClick);
return () => {
window.removeEventListener('click', this.handleClick);
}
});
}
}
```
This can be used to help make even more concise helpers, like:
```js
function useWindowEvent(context, eventName, handler) {
withCleanup(context, () => {
window.addEventListener(eventName, 'click', this.handleClick);
return () => {
window.removeEventListener(eventName, 'click', this.handleClick);
}
});
}
```
So now the above example would be:
```js
class Hello {
constructor() {
useWindowEvent(this, 'click', this.handleClick);
useWindowEvent(this, 'mouseenter', this.handleClick);
}
}
```
### Modifiers
```js
import { eventListeners } from 'ember-lifecycle-utils/modifier';
export default class Hello extends Component {
registerListeners = modifier((element) => {
return eventListeners(element.parentElement,
['click', this.onClick],
['mouseenter', this.onHover],
);
});
// or shorthand
registerListeners = modifier(eventListeners(
['click', this.onClick],
['mouseenter', this.onHover],
));
// ...
}
```
```hbs
click me
```
## Contributing
See the [Contributing](CONTRIBUTING.md) guide for details.
## License
This project is licensed under the [MIT License](LICENSE.md).