Commit 67b8062c authored by Sindre Sorhus's avatar Sindre Sorhus

Merge pull request #861 from Daniel-Hug/$live-improvements

Improve vanilla JS $live: variable names, unnecessary if
parents b31416bc 57ede043
......@@ -7,30 +7,27 @@
window.$$ = document.querySelector.bind(document);
// Register events on elements that may or may not exist yet:
// $live('div a', 'click', function (e) {});
// $live('div a', 'click', function (event) {});
window.$live = (function () {
var eventRegistry = {};
var globalEventDispatcher = function (e) {
var targetElement = e.target;
function dispatchEvent(event) {
var targetElement = event.target;
if (eventRegistry[e.type]) {
eventRegistry[e.type].forEach(function (entry) {
var potentialElements = document.querySelectorAll(entry.selector),
hasMatch = Array.prototype.indexOf.call(potentialElements, targetElement) >= 0;
eventRegistry[event.type].forEach(function (entry) {
var potentialElements = document.querySelectorAll(entry.selector);
var hasMatch = Array.prototype.indexOf.call(potentialElements, targetElement) >= 0;
if (hasMatch) {
entry.handler(e);
}
});
}
};
if (hasMatch) {
entry.handler(event);
}
});
}
return function (selector, event, handler) {
if (!eventRegistry[event]) {
document.documentElement.addEventListener(event, globalEventDispatcher, true);
eventRegistry[event] = [];
document.documentElement.addEventListener(event, dispatchEvent, true);
}
eventRegistry[event].push({
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment