0
0
Fork 0
mirror of https://github.com/liabru/matter-js.git synced 2024-12-27 13:59:01 -05:00

changed Events.on to return callback, changed Events.off to accept callbacks for removal

This commit is contained in:
liabru 2014-04-30 10:32:34 +01:00
parent 43ce567714
commit 5b013d01e7

View file

@ -26,23 +26,43 @@ var Events = {};
object.events[name] = object.events[name] || [];
object.events[name].push(callback);
}
return callback;
};
/**
* Clears all callbacks for the given event names if supplied, otherwise all events
* Removes the given event callback. If no callback, clears all callbacks in eventNames. If no eventNames, clears all events.
* @method off
* @param {} object
* @param {string} eventNames
* @param {function} callback
*/
Events.off = function(object, eventNames) {
Events.off = function(object, eventNames, callback) {
if (!eventNames) {
object.events = {};
return;
}
// handle Events.off(object, callback)
if (typeof eventNames === 'function') {
callback = eventNames;
eventNames = Common.keys(object.events).join(' ');
}
var names = eventNames.split(' ');
for (var i = 0; i < names.length; i++) {
object.events[names[i]] = [];
var callbacks = object.events[names[i]],
newCallbacks = [];
if (callback) {
for (var j = 0; j < callbacks.length; j++) {
if (callbacks[j] !== callback)
newCallbacks.push(callbacks[j]);
}
}
object.events[names[i]] = newCallbacks;
}
};