Created
October 16, 2020 02:42
-
-
Save ayamomiji/7809dd51915a80fe93aeb452d7f16662 to your computer and use it in GitHub Desktop.
componentWillUnmount/beforeDestroy in alpine.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<div x-data @remove='console.log($el, "removed")'></div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const observer = new MutationObserver(mutations => { | |
for (const mutation of mutations) { | |
if (mutation.type !== 'childList') continue | |
mutation.removedNodes.forEach(node => { | |
if (node.nodeType !== Node.ELEMENT_NODE) return | |
[node, ...node.querySelectorAll('[\\@remove]')].forEach(node => { | |
node.dispatchEvent(new CustomEvent('remove')) | |
}) | |
}) | |
} | |
}) | |
observer.observe(document, { | |
childList: true, | |
subtree: true | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment