Fix code sequence when attaching and detaching event handlers.

This commit is contained in:
Jürg Lehni 2011-11-12 17:14:49 +01:00
parent da5a837da2
commit 8eaddc759e

View file

@ -28,11 +28,11 @@ var Callback = {
var handlers = this._handlers = this._handlers || {}; var handlers = this._handlers = this._handlers || {};
handlers = handlers[type] = handlers[type] || []; handlers = handlers[type] = handlers[type] || [];
if (handlers.indexOf(func) == -1) { // Not added yet, add it now 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 // See if this is the first handler that we're attaching, and
// call install if defined. // call install if defined.
if (entry.install && !handlers.length) if (entry.install && handlers.length == 1)
entry.install.call(this); entry.install.call(this);
handlers.push(func);
} }
return this; return this;
}, },
@ -48,17 +48,16 @@ var Callback = {
handlers = this._handlers && this._handlers[type], handlers = this._handlers && this._handlers[type],
index; index;
if (entry && handlers) { if (entry && handlers) {
if (!func) { // Remove all // See if this is the last handler that we're detaching (or if we
handlers = []; // are detaching all handlers), and call uninstall if defined.
} else if ((index = handlers.indexOf(func)) != -1) { if (!func || (index = handlers.indexOf(func)) != -1
handlers.splice(index, 1); && handlers.length == 1) {
}
// See if this is the last handler that we're detaching, and call
// uninstall if defined.
if (!handlers.length) {
delete this._handlers[type];
if (entry.uninstall) if (entry.uninstall)
entry.uninstall.call(this); entry.uninstall.call(this);
delete this._handlers[type];
} else if (index != -1) {
// Just remove this one handler
handlers.splice(index, 1);
} }
} }
return this; return this;