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

$live: improve variables, remove unnecessary if

parent b31416bc
...@@ -7,30 +7,27 @@ ...@@ -7,30 +7,27 @@
window.$$ = document.querySelector.bind(document); window.$$ = document.querySelector.bind(document);
// Register events on elements that may or may not exist yet: // 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 () { window.$live = (function () {
var eventRegistry = {}; var eventRegistry = {};
var globalEventDispatcher = function (e) { function dispatchEvent(event) {
var targetElement = e.target; var targetElement = event.target;
if (eventRegistry[e.type]) { eventRegistry[event.type].forEach(function (entry) {
eventRegistry[e.type].forEach(function (entry) { var potentialElements = document.querySelectorAll(entry.selector);
var potentialElements = document.querySelectorAll(entry.selector), var hasMatch = Array.prototype.indexOf.call(potentialElements, targetElement) >= 0;
hasMatch = Array.prototype.indexOf.call(potentialElements, targetElement) >= 0;
if (hasMatch) { if (hasMatch) {
entry.handler(e); entry.handler(event);
} }
}); });
} }
};
return function (selector, event, handler) { return function (selector, event, handler) {
if (!eventRegistry[event]) { if (!eventRegistry[event]) {
document.documentElement.addEventListener(event, globalEventDispatcher, true);
eventRegistry[event] = []; eventRegistry[event] = [];
document.documentElement.addEventListener(event, dispatchEvent, true);
} }
eventRegistry[event].push({ 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