Henning Snater has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/79776


Change subject: toolbarcontroller: Detach event handlers before re-attaching.
......................................................................

toolbarcontroller: Detach event handlers before re-attaching.

Change-Id: Iaa854768c658f173ca19061e29e7792bf765efda
---
M lib/resources/jquery.wikibase/toolbar/toolbarcontroller.js
1 file changed, 27 insertions(+), 20 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/Wikibase 
refs/changes/76/79776/1

diff --git a/lib/resources/jquery.wikibase/toolbar/toolbarcontroller.js 
b/lib/resources/jquery.wikibase/toolbar/toolbarcontroller.js
index 9833b97..78ef4b5 100644
--- a/lib/resources/jquery.wikibase/toolbar/toolbarcontroller.js
+++ b/lib/resources/jquery.wikibase/toolbar/toolbarcontroller.js
@@ -65,6 +65,7 @@
                 *        (re-)initialization is in a pending state.
                 *
                 * @throws {Error} in case a given toolbar ID is not registered 
for the toolbar type given.
+                * @throws {Error} if the callback provided in an event 
definition is not a function.
                 */
                initToolbars: function( isPending ) {
                        var self = this;
@@ -88,12 +89,28 @@
                                                // definition:
                                                var defaultCallbacks = 
createDefaultEventHandlers( type, options );
 
-                                               // Toolbars that shall be 
created upon certain events.
+                                               // Detach all event handlers 
first in order to not end up with having the
+                                               // handler attached multiple 
times. This cannot be done along with
+                                               // re-attaching the handlers 
since multiple event handlers may be registered
+                                               // for the same event.
+                                               $.each( def.events, function( 
eventNames, callbackOrKeyword ) {
+                                                       $initNode.off( 
self._nameSpace( eventNames, type ) );
+                                               } );
+
+                                               // Attach event handlers for 
toolbars that shall be created upon certain
+                                               // events:
                                                $.each( def.events, function( 
eventNames, callbackOrKeyword ) {
                                                        var callback =
                                                                
defaultCallbacks[ callbackOrKeyword ] || callbackOrKeyword;
 
-                                                       
self._attachToolbarEventListener( $initNode, eventNames, callback );
+                                                       if( !$.isFunction( 
callback ) ) {
+                                                               throw new 
Error( 'No callback or known default action given for '
+                                                                       + 
'event "' + eventNames + '"' );
+                                                       }
+
+                                                       $initNode.on( 
self._nameSpace( eventNames, type ), function( event ) {
+                                                               callback( 
event, $( event.target ) );
+                                                       } );
                                                } );
                                        }
 
@@ -107,27 +124,17 @@
                },
 
                /**
-                * Attaches an event listener defined by the toolbar definition 
to the node the toolbar is
-                * initialized on.
+                * Applies namespaces to event names passed in as a string.
                 * @since 0.4
                 *
-                * @param {jQuery} $toolbar
-                * @param {string[]} eventNames
-                * @param {Function} callback
-                *
-                * @throws {Error} if the provided callback is not a function.
+                * @param {string} eventNames
+                * @param {string} toolbarType
+                * @return {string}
                 */
-               _attachToolbarEventListener: function( $toolbar, eventNames, 
callback ) {
-                       if( !$.isFunction( callback ) ) {
-                               throw new Error( 'No callback or known default 
action given for event "' +
-                                       eventNames + '"' );
-                       }
-                       var namespacedEventNames = ( eventNames + ' ' )
-                               .split( ' ' ).join( '.' + this.widgetName + ' ' 
);
-
-                       $toolbar.on( namespacedEventNames, function( event ) {
-                               callback( event, $( event.target ) );
-                       } );
+               _nameSpace: function( eventNames, toolbarType ) {
+                       return ( eventNames + ' ' )
+                               .split( ' ' )
+                               .join( '.' + this.widgetName + '.' + 
toolbarType + ' ' );
                },
 
                /**

-- 
To view, visit https://gerrit.wikimedia.org/r/79776
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: Iaa854768c658f173ca19061e29e7792bf765efda
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/Wikibase
Gerrit-Branch: master
Gerrit-Owner: Henning Snater <henning.sna...@wikimedia.de>

_______________________________________________
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to