jenkins-bot has submitted this change and it was merged.

Change subject: mediawiki.widgets: Remove use of bind() for lexical 'this' 
binding
......................................................................


mediawiki.widgets: Remove use of bind() for lexical 'this' binding

Follows-up 4636ac79dd.

Bind can be useful when needing to pass an instance method elewhere.

However when nesting closures, use the scope directly instead of binding 'this'
several layers deep. This is fragile at best and doesn't make it less confusing.

Leave the natural 'this' unchanged. Change this can go wrong both ways and
results in unpredictable behaviour and confusing code that is hard to review.
Sometimes one means the outer 'this' but gets the inner one, and sometimes you
need the inner one (e.g. inside callbacks for jQuery). Consistently assign a
variable and use scope to access objects. Besides, one can't escape it when you
need both.

This avoids an entire class of potential errors. It also performs marginally
better without a binding but that's besides the point as there are other valid
uses of bind().

Change-Id: I1fcfdbd8fa7c52e150cadd8a520591e700c5bfa9
---
M resources/src/mediawiki.widgets/mw.widgets.CategoryCapsuleItemWidget.js
1 file changed, 16 insertions(+), 14 deletions(-)

Approvals:
  Fomafix: Looks good to me, but someone else must approve
  Bartosz Dziewoński: Looks good to me, approved
  jenkins-bot: Verified



diff --git 
a/resources/src/mediawiki.widgets/mw.widgets.CategoryCapsuleItemWidget.js 
b/resources/src/mediawiki.widgets/mw.widgets.CategoryCapsuleItemWidget.js
index 5369d35..58115c3 100644
--- a/resources/src/mediawiki.widgets/mw.widgets.CategoryCapsuleItemWidget.js
+++ b/resources/src/mediawiki.widgets/mw.widgets.CategoryCapsuleItemWidget.js
@@ -25,23 +25,24 @@
         * @private
         */
        PageExistenceCache.prototype.processExistenceCheckQueue = function () {
-               var queue, titles;
+               var queue, titles,
+                       cache = this;
                if ( this.currentRequest ) {
                        // Don't fire off a million requests at the same time
                        this.currentRequest.always( function () {
-                               this.currentRequest = null;
-                               this.processExistenceCheckQueueDebounced();
-                       }.bind( this ) );
+                               cache.currentRequest = null;
+                               cache.processExistenceCheckQueueDebounced();
+                       } );
                        return;
                }
                queue = this.existenceCheckQueue;
                this.existenceCheckQueue = {};
                titles = Object.keys( queue ).filter( function ( title ) {
-                       if ( this.existenceCache.hasOwnProperty( title ) ) {
-                               queue[ title ].resolve( this.existenceCache[ 
title ] );
+                       if ( cache.existenceCache.hasOwnProperty( title ) ) {
+                               queue[ title ].resolve( cache.existenceCache[ 
title ] );
                        }
-                       return !this.existenceCache.hasOwnProperty( title );
-               }.bind( this ) );
+                       return !cache.existenceCache.hasOwnProperty( title );
+               } );
                if ( !titles.length ) {
                        return;
                }
@@ -53,10 +54,10 @@
                } ).done( function ( response ) {
                        $.each( response.query.pages, function ( index, page ) {
                                var title = new ForeignTitle( page.title 
).getPrefixedText();
-                               this.existenceCache[ title ] = !page.missing;
-                               queue[ title ].resolve( this.existenceCache[ 
title ] );
-                       }.bind( this ) );
-               }.bind( this ) );
+                               cache.existenceCache[ title ] = !page.missing;
+                               queue[ title ].resolve( cache.existenceCache[ 
title ] );
+                       } );
+               } );
        };
 
        /**
@@ -107,6 +108,7 @@
         * @cfg {string} [apiUrl] API URL, if not the current wiki's API
         */
        mw.widgets.CategoryCapsuleItemWidget = function 
MWWCategoryCapsuleItemWidget( config ) {
+               var widget = this;
                // Parent constructor
                mw.widgets.CategoryCapsuleItemWidget.parent.call( this, 
$.extend( {
                        data: config.title.getMainText(),
@@ -137,8 +139,8 @@
                this.constructor.static.pageExistenceCaches[ this.apiUrl ]
                        .checkPageExistence( new ForeignTitle( 
this.title.getPrefixedText() ) )
                        .done( function ( exists ) {
-                               this.setMissing( !exists );
-                       }.bind( this ) );
+                               widget.setMissing( !exists );
+                       } );
                /*jshint +W024*/
        };
 

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

Gerrit-MessageType: merged
Gerrit-Change-Id: I1fcfdbd8fa7c52e150cadd8a520591e700c5bfa9
Gerrit-PatchSet: 2
Gerrit-Project: mediawiki/core
Gerrit-Branch: master
Gerrit-Owner: Krinkle <[email protected]>
Gerrit-Reviewer: Bartosz Dziewoński <[email protected]>
Gerrit-Reviewer: Edokter <[email protected]>
Gerrit-Reviewer: Fomafix
Gerrit-Reviewer: Jack Phoenix <[email protected]>
Gerrit-Reviewer: Jforrester <[email protected]>
Gerrit-Reviewer: MarkTraceur <[email protected]>
Gerrit-Reviewer: jenkins-bot <>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to