Commit 9012ec86 authored by Sam Saccone's avatar Sam Saccone

Merge pull request #1221 from evverx/direct-delegate

Delegate events to #todo-list instead of document
parents 96638935 822adc1a
...@@ -15,36 +15,24 @@ ...@@ -15,36 +15,24 @@
target.addEventListener(type, callback, !!useCapture); target.addEventListener(type, callback, !!useCapture);
}; };
// Register events on elements that may or may not exist yet: // Attach a handler to event for all elements that match the selector,
// $live('div a', 'click', function (event) {}); // now or in the future, based on a root element
window.$live = (function () { window.$delegate = function (target, selector, type, handler) {
var eventRegistry = {};
function dispatchEvent(event) { function dispatchEvent(event) {
var targetElement = event.target; var targetElement = event.target;
var potentialElements = window.qsa(selector, target);
eventRegistry[event.type].forEach(function (entry) {
var potentialElements = window.qsa(entry.selector);
var hasMatch = Array.prototype.indexOf.call(potentialElements, targetElement) >= 0; var hasMatch = Array.prototype.indexOf.call(potentialElements, targetElement) >= 0;
if (hasMatch) { if (hasMatch) {
entry.handler.call(targetElement, event); handler.call(targetElement, event);
} }
});
} }
return function (selector, event, handler) { // https://developer.mozilla.org/en-US/docs/Web/Events/blur
if (!eventRegistry[event]) { var useCapture = type === 'blur' || type === 'focus';
eventRegistry[event] = [];
window.$on(document.documentElement, event, dispatchEvent, true);
}
eventRegistry[event].push({ window.$on(target, type, dispatchEvent, useCapture);
selector: selector,
handler: handler
});
}; };
}());
// Find the element's parent with the given tag name: // Find the element's parent with the given tag name:
// $parent(qs('a'), 'div'); // $parent(qs('a'), 'div');
......
/*global qs, qsa, $on, $parent, $live */ /*global qs, qsa, $on, $parent, $delegate */
(function (window) { (function (window) {
'use strict'; 'use strict';
...@@ -140,7 +140,7 @@ ...@@ -140,7 +140,7 @@
View.prototype._bindItemEditDone = function (handler) { View.prototype._bindItemEditDone = function (handler) {
var that = this; var that = this;
$live('#todo-list li .edit', 'blur', function () { $delegate(that.$todoList, 'li .edit', 'blur', function () {
if (!this.dataset.iscanceled) { if (!this.dataset.iscanceled) {
handler({ handler({
id: that._itemId(this), id: that._itemId(this),
...@@ -149,7 +149,7 @@ ...@@ -149,7 +149,7 @@
} }
}); });
$live('#todo-list li .edit', 'keypress', function (event) { $delegate(that.$todoList, 'li .edit', 'keypress', function (event) {
if (event.keyCode === that.ENTER_KEY) { if (event.keyCode === that.ENTER_KEY) {
// Remove the cursor from the input when you hit enter just like if it // Remove the cursor from the input when you hit enter just like if it
// were a real form // were a real form
...@@ -160,7 +160,7 @@ ...@@ -160,7 +160,7 @@
View.prototype._bindItemEditCancel = function (handler) { View.prototype._bindItemEditCancel = function (handler) {
var that = this; var that = this;
$live('#todo-list li .edit', 'keyup', function (event) { $delegate(that.$todoList, 'li .edit', 'keyup', function (event) {
if (event.keyCode === that.ESCAPE_KEY) { if (event.keyCode === that.ESCAPE_KEY) {
this.dataset.iscanceled = true; this.dataset.iscanceled = true;
this.blur(); this.blur();
...@@ -188,17 +188,17 @@ ...@@ -188,17 +188,17 @@
}); });
} else if (event === 'itemEdit') { } else if (event === 'itemEdit') {
$live('#todo-list li label', 'dblclick', function () { $delegate(that.$todoList, 'li label', 'dblclick', function () {
handler({id: that._itemId(this)}); handler({id: that._itemId(this)});
}); });
} else if (event === 'itemRemove') { } else if (event === 'itemRemove') {
$live('#todo-list .destroy', 'click', function () { $delegate(that.$todoList, '.destroy', 'click', function () {
handler({id: that._itemId(this)}); handler({id: that._itemId(this)});
}); });
} else if (event === 'itemToggle') { } else if (event === 'itemToggle') {
$live('#todo-list .toggle', 'click', function () { $delegate(that.$todoList, '.toggle', 'click', function () {
handler({ handler({
id: that._itemId(this), id: that._itemId(this),
completed: this.checked completed: this.checked
......
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