TitleSearchWidget: Use OO.ui.mixin.RequestManager
authorDavid Lynch <dlynch@wikimedia.org>
Wed, 11 Nov 2015 22:54:53 +0000 (14:54 -0800)
committerDavid Lynch <dlynch@wikimedia.org>
Thu, 19 Nov 2015 19:48:03 +0000 (13:48 -0600)
Get that request management goodness that only LookupWidgets used to
benefit from. We can undo the earlier hacky fix to avoid requests
overrunning each other as part of this.

Bug: T114175
Change-Id: I6ff686b3b157d23ff534357ce25ebcb4ef7efea1

resources/src/mediawiki.widgets/mw.widgets.TitleSearchWidget.js

index 546fbf8..96f9549 100644 (file)
@@ -11,6 +11,7 @@
         *
         * @class
         * @extends OO.ui.SearchWidget
+        * @mixins OO.ui.mixin.RequestManager
         * @mixins mw.widgets.TitleWidget
         *
         * @constructor
@@ -23,6 +24,7 @@
 
                // Mixin constructors
                mw.widgets.TitleWidget.call( this, config );
+               OO.ui.mixin.RequestManager.call( this, config );
 
                this.query.setValidation( this.isQueryValid.bind( this ) );
 
@@ -46,6 +48,7 @@
        /* Setup */
 
        OO.inheritClass( mw.widgets.TitleSearchWidget, OO.ui.SearchWidget );
+       OO.mixinClass( mw.widgets.TitleSearchWidget, OO.ui.mixin.RequestManager );
        OO.mixinClass( mw.widgets.TitleSearchWidget, mw.widgets.TitleWidget );
 
        /* Methods */
        mw.widgets.TitleSearchWidget.prototype.onQueryChange = function () {
                var widget = this;
 
-               if ( this.currentRequest ) {
-                       this.currentRequest.abort();
-               }
-
-               this.currentRequest = this.getSuggestionsPromise();
-               this.currentRequest.done( function ( response ) {
+               this.getRequestData().done( function ( data ) {
                        // Parent method
                        mw.widgets.TitleSearchWidget.parent.prototype.onQueryChange.call( widget );
-
-                       widget.results.addItems( widget.getOptionsFromData( response.query || {} ) );
-
-                       widget.currentRequest = false;
+                       widget.results.addItems( widget.getOptionsFromData( data ) );
                } );
        };
 
+       /**
+        * @inheritdoc OO.ui.mixin.RequestManager
+        */
+       mw.widgets.TitleSearchWidget.prototype.getRequestQuery = function () {
+               return this.getQueryValue();
+       };
+       /**
+        * @inheritdoc OO.ui.mixin.RequestManager
+        */
+       mw.widgets.TitleSearchWidget.prototype.getRequest = function () {
+               return this.getSuggestionsPromise();
+       };
+       /**
+        * @inheritdoc OO.ui.mixin.RequestManager
+        */
+       mw.widgets.TitleSearchWidget.prototype.getRequestCacheDataFromResponse = function ( response ) {
+               return response.query || {};
+       };
+
 }( jQuery, mediaWiki ) );