From 10fbca1549ac568e113af13557ffbf6b6e0422c5 Mon Sep 17 00:00:00 2001 From: Svein-Tore Griff With Date: Mon, 16 Feb 2015 14:22:54 +0100 Subject: [PATCH] Documentation updates --- js/event-dispatcher.js | 33 +++++++++++++++++++-------------- 1 file changed, 19 insertions(+), 14 deletions(-) diff --git a/js/event-dispatcher.js b/js/event-dispatcher.js index 7a86962..36066ca 100644 --- a/js/event-dispatcher.js +++ b/js/event-dispatcher.js @@ -1,6 +1,10 @@ /** @namespace H5P */ var H5P = H5P || {}; +/** + * The Event class for the EventDispatcher + * @class + */ H5P.Event = function(type, data) { this.type = type; this.data = data; @@ -27,13 +31,14 @@ H5P.EventDispatcher = (function () { * Add new event listener. * * @public - * @throws {TypeError} listener must be a function - * @param {String} type Event type - * @param {Function} listener Event listener + * @throws {TypeError} listener - Must be a function + * @param {String} type - Event type + * @param {Function} listener - Event listener + * @param {Function} thisArg - Optional thisArg to call the listener from */ - self.on = function (type, listener, scope) { - if (scope === undefined) { - scope = self; + self.on = function (type, listener, thisArg) { + if (thisArg === undefined) { + thisArg = self; } if (!(listener instanceof Function)) { throw TypeError('listener must be a function'); @@ -44,11 +49,11 @@ H5P.EventDispatcher = (function () { if (!triggers[type]) { // First - triggers[type] = [{'listener': listener, 'scope': scope}]; + triggers[type] = [{'listener': listener, 'thisArg': thisArg}]; } else { // Append - triggers[type].push({'listener': listener, 'scope': scope}); + triggers[type].push({'listener': listener, 'thisArg': thisArg}); } }; @@ -60,9 +65,9 @@ H5P.EventDispatcher = (function () { * @param {String} type Event type * @param {Function} listener Event listener */ - self.once = function (type, listener, scope) { - if (scope === undefined) { - scope = self; + self.once = function (type, listener, thisArg) { + if (thisArg === undefined) { + thisArg = self; } if (!(listener instanceof Function)) { throw TypeError('listener must be a function'); @@ -70,10 +75,10 @@ H5P.EventDispatcher = (function () { var once = function (event) { self.off(event, once); - listener.apply(scope, event); + listener.apply(thisArg, event); }; - self.on(type, once, scope); + self.on(type, once, thisArg); }; /** @@ -138,7 +143,7 @@ H5P.EventDispatcher = (function () { } // Call all listeners for (var i = 0; i < triggers[event.type].length; i++) { - triggers[event.type][i].listener.call(triggers[event.type][i].scope, event); + triggers[event.type][i].listener.call(triggers[event.type][i].thisArg, event); } }; }