| Module: | @ember/destroyable |
|---|
Defined in packages/@ember/destroyable/index.ts:164
import { registerDestructor } from '@ember/destroyable'; Receives a destroyable object and a destructor function, and associates the function with it. When the destroyable is destroyed with destroy, or when its parent is destroyed, the destructor function will be called.
import Component from '@glimmer/component';
import { registerDestructor } from '@ember/destroyable';
class Modal extends Component {
@service resize;
constructor(...args) {
super(...args);
this.resize.register(this, this.layout);
registerDestructor(this, () => this.resize.unregister(this));
}
} Multiple destructors can be associated with a given destroyable, and they can be associated over time, allowing libraries to dynamically add destructors as needed. registerDestructor also returns the associated destructor function, for convenience.
The destructor function is passed a single argument, which is the destroyable itself. This allows the function to be reused multiple times for many destroyables, rather than creating a closure function per destroyable.
import Component from '@glimmer/component';
import { registerDestructor } from '@ember/destroyable';
function unregisterResize(instance) {
instance.resize.unregister(instance);
}
class Modal extends Component {
@service resize;
constructor(...args) {
super(...args);
this.resize.register(this, this.layout);
registerDestructor(this, unregisterResize);
}
}
© 2022 Yehuda Katz, Tom Dale and Ember.js contributors
Licensed under the MIT License.
https://api.emberjs.com/ember/4.9/functions/@ember%2Fdestroyable/registerDestructor