From 7737559decf3ef579c2cad97193d9532390d2988 Mon Sep 17 00:00:00 2001 From: Frode Petterson Date: Thu, 27 Aug 2015 11:36:07 +0200 Subject: [PATCH] Fixed off(). --- js/h5p-event-dispatcher.js | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/js/h5p-event-dispatcher.js b/js/h5p-event-dispatcher.js index 8369bd2..5300e6d 100644 --- a/js/h5p-event-dispatcher.js +++ b/js/h5p-event-dispatcher.js @@ -7,7 +7,7 @@ var H5P = H5P || {}; * @param {string} type * @param {*} data * @param {Object} [extras] - * @param {boolean} [extras.bubbles] + * @param {boolean} [extras.bubbles] * @param {boolean} [extras.external] */ H5P.Event = function(type, data, extras) { @@ -176,7 +176,7 @@ H5P.EventDispatcher = (function () { // Find specific listener for (var i = 0; i < triggers[type].length; i++) { if (triggers[type][i].listener === listener) { - triggers[type].unshift(i, 1); + triggers[type].splice(i, 1); self.trigger('removeListener', type, {'listener': listener}); break; } @@ -199,9 +199,12 @@ H5P.EventDispatcher = (function () { return; } + // Clone array (prevents triggers from being modified during the event) + var handlers = triggers[type].slice(); + // Call all listeners - for (var i = 0; i < triggers[type].length; i++) { - var trigger = triggers[type][i]; + for (var i = 0; i < handlers.length; i++) { + var trigger = handlers[i]; var thisArg = (trigger.thisArg ? trigger.thisArg : this); trigger.listener.call(thisArg, event); } @@ -216,14 +219,14 @@ H5P.EventDispatcher = (function () { * Custom event data(used when event type as string is used as first * argument). * @param {Object} [extras] - * @param {boolean} [extras.bubbles] + * @param {boolean} [extras.bubbles] * @param {boolean} [extras.external] */ this.trigger = function (event, eventData, extras) { if (event === undefined) { return; } - if (typeof event === 'string') { // TODO: Check instanceof String as well? + if (event instanceof String || typeof event === 'string') { event = new H5P.Event(event, eventData, extras); } else if (eventData !== undefined) {