An open API service indexing awesome lists of open source software.

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

Awesome Lists containing this project

README

          

# ember-lifecycle-utils

[![npm version](https://badge.fury.io/js/ember-lifecycle-utils.svg)](https://badge.fury.io/js/ember-lifecycle-utils)
[![CI](https://github.com/NullVoxPopuli/ember-lifecycle-utils/actions/workflows/ci.yml/badge.svg?branch=main&event=push)](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).