Commit 57ede043 authored by Daniel Hug's avatar Daniel Hug

$live: improve variables, remove unnecessary if

parent b31416bc
......@@ -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