From f8434bf7883e899dcfe1d3fe0e80b6afd4af3840 Mon Sep 17 00:00:00 2001 From: Svein-Tore Griff With Date: Wed, 11 Feb 2015 17:31:01 +0100 Subject: [PATCH] Add scopes to event system --- js/event-dispatcher.js | 30 ++++++++++++++++++------------ 1 file changed, 18 insertions(+), 12 deletions(-) diff --git a/js/event-dispatcher.js b/js/event-dispatcher.js index e9152b0..7a86962 100644 --- a/js/event-dispatcher.js +++ b/js/event-dispatcher.js @@ -31,21 +31,24 @@ H5P.EventDispatcher = (function () { * @param {String} type Event type * @param {Function} listener Event listener */ - self.on = function (type, listener) { + self.on = function (type, listener, scope) { + if (scope === undefined) { + scope = self; + } if (!(listener instanceof Function)) { throw TypeError('listener must be a function'); } // Trigger event before adding to avoid recursion - self.trigger('newListener', type, listener); + self.trigger('newListener', {'type': type, 'listener': listener}); if (!triggers[type]) { // First - triggers[type] = [listener]; + triggers[type] = [{'listener': listener, 'scope': scope}]; } else { // Append - triggers[type].push(listener); + triggers[type].push({'listener': listener, 'scope': scope}); } }; @@ -57,17 +60,20 @@ H5P.EventDispatcher = (function () { * @param {String} type Event type * @param {Function} listener Event listener */ - self.once = function (type, listener) { + self.once = function (type, listener, scope) { + if (scope === undefined) { + scope = self; + } if (!(listener instanceof Function)) { throw TypeError('listener must be a function'); } - var once = function () { - self.off(type, once); - listener.apply(self, arguments); + var once = function (event) { + self.off(event, once); + listener.apply(scope, event); }; - self.on(type, once); + self.on(type, once, scope); }; /** @@ -97,9 +103,9 @@ H5P.EventDispatcher = (function () { // Find specific listener for (var i = 0; i < triggers[type].length; i++) { - if (triggers[type][i] === listener) { + if (triggers[type][i].listener === listener) { triggers[type].unshift(i, 1); - self.trigger('removeListener', type, listener); + self.trigger('removeListener', type, {'listener': listener}); break; } } @@ -132,7 +138,7 @@ H5P.EventDispatcher = (function () { } // Call all listeners for (var i = 0; i < triggers[event.type].length; i++) { - triggers[event.type][i].call(self, event); + triggers[event.type][i].listener.call(triggers[event.type][i].scope, event); } }; }