Attach and Detach Handlers
Implement Attachable
to get notified when an element is attached to and detached from the DOM tree. The attachable interface provides a static method to register the callbacks easily attach(MutationRecord)
and detach(MutationRecord)
:
Elemento uses the MutationObserver
API to detect changes in the DOM tree and passes an MutationRecord
instance to the attach(MutationRecord)
and detach(MutationRecord)
methods. This instance contains additional information about the DOM manipulation.
The attach(MutationRecord)
and detach(MutationRecord)
methods are only called once after the attachable implementation has been registered with Attachable.register(HTMLElement, Attachable)
. If you hold a reference to the element, and the element is attached to the DOM again, the callbacks are not called again unless the element has been registered again. This is done for reasons of optimisation and performance.
If you want to be notified every time your custom element is attached to the DOM, it is recommended to add the call to Attachable.register(HTMLElement, Attachable)
in the constructor, like in the example above, and recreate the custom element.
Last updated