Documentation updates

d6
Svein-Tore Griff With 2015-02-16 14:22:54 +01:00
parent 84f8fb1cff
commit 10fbca1549
1 changed files with 19 additions and 14 deletions

View File

@ -1,6 +1,10 @@
/** @namespace H5P */ /** @namespace H5P */
var H5P = H5P || {}; var H5P = H5P || {};
/**
* The Event class for the EventDispatcher
* @class
*/
H5P.Event = function(type, data) { H5P.Event = function(type, data) {
this.type = type; this.type = type;
this.data = data; this.data = data;
@ -27,13 +31,14 @@ H5P.EventDispatcher = (function () {
* Add new event listener. * Add new event listener.
* *
* @public * @public
* @throws {TypeError} listener must be a function * @throws {TypeError} listener - Must be a function
* @param {String} type Event type * @param {String} type - Event type
* @param {Function} listener Event listener * @param {Function} listener - Event listener
* @param {Function} thisArg - Optional thisArg to call the listener from
*/ */
self.on = function (type, listener, scope) { self.on = function (type, listener, thisArg) {
if (scope === undefined) { if (thisArg === undefined) {
scope = self; thisArg = self;
} }
if (!(listener instanceof Function)) { if (!(listener instanceof Function)) {
throw TypeError('listener must be a function'); throw TypeError('listener must be a function');
@ -44,11 +49,11 @@ H5P.EventDispatcher = (function () {
if (!triggers[type]) { if (!triggers[type]) {
// First // First
triggers[type] = [{'listener': listener, 'scope': scope}]; triggers[type] = [{'listener': listener, 'thisArg': thisArg}];
} }
else { else {
// Append // 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 {String} type Event type
* @param {Function} listener Event listener * @param {Function} listener Event listener
*/ */
self.once = function (type, listener, scope) { self.once = function (type, listener, thisArg) {
if (scope === undefined) { if (thisArg === undefined) {
scope = self; thisArg = self;
} }
if (!(listener instanceof Function)) { if (!(listener instanceof Function)) {
throw TypeError('listener must be a function'); throw TypeError('listener must be a function');
@ -70,10 +75,10 @@ H5P.EventDispatcher = (function () {
var once = function (event) { var once = function (event) {
self.off(event, once); 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 // Call all listeners
for (var i = 0; i < triggers[event.type].length; i++) { 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);
} }
}; };
} }