我将一些功能附加到 DOM 元素,并希望能够在元素从 DOM 中删除时清除所有引用,以便它可以被垃圾收集,
我检测元素删除的初始版本是这样的:
var onremove = function(element, callback) {
var destroy = function() {
callback();
element.removeEventListener('DOMNodeRemovedFromDocument', destroy);
};
element.addEventListener('DOMNodeRemovedFromDocument', destroy);
};
然后我读到mutation events已弃用 MutationObserver .所以我尝试移植我的代码。这是我想出的:
var isDescendant = function(desc, root) {
return !!desc && (desc === root || isDecendant(desc.parentNode, root));
};
var onremove = function(element, callback) {
var observer = new MutationObserver(function(mutations) {
_.forEach(mutations, function(mutation) {
_.forEach(mutation.removedNodes, function(removed) {
if (isDescendant(element, removed)) {
callback();
// allow garbage collection
observer.disconnect();
observer = undefined;
}
});
});
});
observer.observe(document, {
childList: true,
subtree: true
});
};
这对我来说看起来过于复杂(而且效率不高)。我是不是遗漏了什么,或者这真的是它应该工作的方式吗?
最佳答案
其实...是的,有一个更优雅的解决方案:)。
您添加的内容看起来不错,而且似乎经过了很好的优化。然而,有一种更简单的方法可以知道节点是否附加到 DOM。
function onRemove(element, onDetachCallback) {
const observer = new MutationObserver(function () {
function isDetached(el) {
if (el.parentNode === document) {
return false;
} else if (el.parentNode === null) {
return true;
} else {
return isDetached(el.parentNode);
}
}
if (isDetached(element)) {
observer.disconnect();
onDetachCallback();
}
})
observer.observe(document, {
childList: true,
subtree: true
});
}
关于javascript - DOMNodeRemovedFromDocument 的简单 MutationObserver 版本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31798816/