unregistered events enabled in emitter

This commit is contained in:
rumman 2015-04-07 22:33:48 +02:00
parent 7749641e66
commit 8b24a33b17

View file

@ -23,19 +23,17 @@ var Emitter = {
this.on(key, value); this.on(key, value);
}, this); }, this);
} else { } else {
var entry = this._eventTypes[type]; var entry = this._eventTypes[type],
if (entry) { handlers = this._callbacks = this._callbacks || {};
var handlers = this._callbacks = this._callbacks || {};
handlers = handlers[type] = handlers[type] || []; handlers = handlers[type] = handlers[type] || [];
if (handlers.indexOf(func) === -1) { // Not added yet, add now. if (handlers.indexOf(func) === -1) { // Not added yet, add now.
handlers.push(func); handlers.push(func);
// See if this is the first handler that we're attaching, // See if this is the first handler that we're attaching,
// and call install if defined. // and call install if defined.
if (entry.install && handlers.length == 1) if (entry && entry.install && handlers.length == 1)
entry.install.call(this, type); entry.install.call(this, type);
} }
} }
}
return this; return this;
}, },
@ -50,12 +48,12 @@ var Emitter = {
var entry = this._eventTypes[type], var entry = this._eventTypes[type],
handlers = this._callbacks && this._callbacks[type], handlers = this._callbacks && this._callbacks[type],
index; index;
if (entry && handlers) { if (handlers) {
// See if this is the last handler that we're detaching (or if we // See if this is the last handler that we're detaching (or if we
// are detaching all handlers), and call uninstall if defined. // are detaching all handlers), and call uninstall if defined.
if (!func || (index = handlers.indexOf(func)) !== -1 if (!func || (index = handlers.indexOf(func)) !== -1
&& handlers.length === 1) { && handlers.length === 1) {
if (entry.uninstall) if (entry && entry.uninstall)
entry.uninstall.call(this, type); entry.uninstall.call(this, type);
delete this._callbacks[type]; delete this._callbacks[type];
} else if (index !== -1) { } else if (index !== -1) {
@ -106,7 +104,7 @@ var Emitter = {
for (var type in handlers) { for (var type in handlers) {
if (handlers[type].length > 0) { if (handlers[type].length > 0) {
var entry = this._eventTypes[type], var entry = this._eventTypes[type],
func = entry[key]; func = entry && entry[key];
if (func) if (func)
func.call(this, type); func.call(this, type);
} }