Commit 6a470b7c authored by Tom Dale's avatar Tom Dale

Merge pull request #5 from alassek/multiple-events

EventTarget: set handler for multiple events at once
parents 748277e7 1f80305e
......@@ -78,10 +78,12 @@ var EventTarget = exports.EventTarget = {
return object;
},
on: function(eventName, callback, binding) {
var allCallbacks = callbacksFor(this), callbacks;
on: function(eventNames, callback, binding) {
var allCallbacks = callbacksFor(this), callbacks, eventName;
eventNames = eventNames.split(/\s+/);
binding = binding || this;
while (eventName = eventNames.shift()) {
callbacks = allCallbacks[eventName];
if (!callbacks) {
......@@ -91,21 +93,25 @@ var EventTarget = exports.EventTarget = {
if (indexOf(callbacks, callback) === -1) {
callbacks.push([callback, binding]);
}
}
},
off: function(eventName, callback) {
var allCallbacks = callbacksFor(this), callbacks;
off: function(eventNames, callback) {
var allCallbacks = callbacksFor(this), callbacks, eventName, index;
eventNames = eventNames.split(/\s+/);
while (eventName = eventNames.shift()) {
if (!callback) {
allCallbacks[eventName] = [];
return;
continue;
}
callbacks = allCallbacks[eventName];
var index = indexOf(callbacks, callback);
index = indexOf(callbacks, callback);
if (index !== -1) { callbacks.splice(index, 1); }
}
},
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