Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save mikermcneil/e7a799fc300fb462a88a19ba74e6a01d to your computer and use it in GitHub Desktop.
Save mikermcneil/e7a799fc300fb462a88a19ba74e6a01d to your computer and use it in GitHub Desktop.
// Docs:
// https://developer.mozilla.org/en-US/docs/Web/API/MutationObserver#MutationObserverInit
//
// This is what it actually means:
// =========================================================================================================
var $elementToStretch = $('#the-element');
var debouncedStretchInnards = _.debounce(stretchInnards);
var $allAncestors = $elementToStretch.parents();
editorMD.ancestorObservers = _.reduce($allAncestors, function (memo, ancestorRawDOMElement) {
var observer = new MutationObserver(function onMutation(){ debouncedStretchInnards(); });
observer.observe(ancestorRawDOMElement, { attributes: true, attributeOldValue: true, attributeFilter: ['style'] });
// Track ancestor observer.
memo.push(observer);
return memo;
}, []);
// Then in onRemove, call `.disconnect()` on each "observer" in `editorMD.ancestorObservers`.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment