From 8eaddc759edbf1acc98cd961c98b612b8b71d8ed Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=BCrg=20Lehni?= Date: Sat, 12 Nov 2011 17:14:49 +0100 Subject: [PATCH] Fix code sequence when attaching and detaching event handlers. --- src/core/Callback.js | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/src/core/Callback.js b/src/core/Callback.js index 79e54163..29a4aecc 100644 --- a/src/core/Callback.js +++ b/src/core/Callback.js @@ -28,11 +28,11 @@ var Callback = { var handlers = this._handlers = this._handlers || {}; handlers = handlers[type] = handlers[type] || []; if (handlers.indexOf(func) == -1) { // Not added yet, add it now + handlers.push(func); // See if this is the first handler that we're attaching, and // call install if defined. - if (entry.install && !handlers.length) + if (entry.install && handlers.length == 1) entry.install.call(this); - handlers.push(func); } return this; }, @@ -48,17 +48,16 @@ var Callback = { handlers = this._handlers && this._handlers[type], index; if (entry && handlers) { - if (!func) { // Remove all - handlers = []; - } else if ((index = handlers.indexOf(func)) != -1) { - handlers.splice(index, 1); - } - // See if this is the last handler that we're detaching, and call - // uninstall if defined. - if (!handlers.length) { - delete this._handlers[type]; + // See if this is the last handler that we're detaching (or if we + // are detaching all handlers), and call uninstall if defined. + if (!func || (index = handlers.indexOf(func)) != -1 + && handlers.length == 1) { if (entry.uninstall) entry.uninstall.call(this); + delete this._handlers[type]; + } else if (index != -1) { + // Just remove this one handler + handlers.splice(index, 1); } } return this;