Commit 1f80305e authored by Adam Lassek's avatar Adam Lassek

EventTarget: set handler for multiple events at once

parent a69b2dcf
...@@ -78,34 +78,40 @@ var EventTarget = exports.EventTarget = { ...@@ -78,34 +78,40 @@ var EventTarget = exports.EventTarget = {
return object; return object;
}, },
on: function(eventName, callback, binding) { on: function(eventNames, callback, binding) {
var allCallbacks = callbacksFor(this), callbacks; var allCallbacks = callbacksFor(this), callbacks, eventName;
eventNames = eventNames.split(/\s+/);
binding = binding || this; binding = binding || this;
callbacks = allCallbacks[eventName]; while (eventName = eventNames.shift()) {
callbacks = allCallbacks[eventName];
if (!callbacks) { if (!callbacks) {
callbacks = allCallbacks[eventName] = []; callbacks = allCallbacks[eventName] = [];
} }
if (indexOf(callbacks, callback) === -1) { if (indexOf(callbacks, callback) === -1) {
callbacks.push([callback, binding]); callbacks.push([callback, binding]);
}
} }
}, },
off: function(eventName, callback) { off: function(eventNames, callback) {
var allCallbacks = callbacksFor(this), callbacks; var allCallbacks = callbacksFor(this), callbacks, eventName, index;
eventNames = eventNames.split(/\s+/);
if (!callback) { while (eventName = eventNames.shift()) {
allCallbacks[eventName] = []; if (!callback) {
return; allCallbacks[eventName] = [];
} continue;
}
callbacks = allCallbacks[eventName]; callbacks = allCallbacks[eventName];
var index = indexOf(callbacks, callback); index = indexOf(callbacks, callback);
if (index !== -1) { callbacks.splice(index, 1); } if (index !== -1) { callbacks.splice(index, 1); }
}
}, },
trigger: function(eventName, options) { trigger: function(eventName, options) {
......
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