Skip to content

Instantly share code, notes, and snippets.

@treshugart
Last active February 23, 2017 01:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save treshugart/fbc37ab7d19028b9fe22311bd1b634f4 to your computer and use it in GitHub Desktop.
Save treshugart/fbc37ab7d19028b9fe22311bd1b634f4 to your computer and use it in GitHub Desktop.
Setting context props at specific DOM tree levels on elements.
const _context = Symbol();
let currentContext = null;
function getContext (elem) {
elem.dispatchEvent(new Event('__context', {
bubbles: true,
cancelable: true,
composed: true,
scoped: true
}));
const returnContext = currentContext;
currentContext = null;
return returnContext;
}
function setContext (elem, context) {
if (!elem.hasOwnProperty(_context)) {
elem.addEventListener('__context', function (e) {
e.stopPropagation();
currentContext = this[_context];
});
}
elem[_context] = context;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment