mediawiki.searchSuggest: Code clean up
authorTimo Tijhof <krinklemail@gmail.com>
Wed, 21 May 2014 01:07:08 +0000 (03:07 +0200)
committerKrinkle <krinklemail@gmail.com>
Wed, 21 May 2014 01:20:34 +0000 (01:20 +0000)
* Cache mw.Api instance.
* Remove redundant "str.length" check in fetch(),
  jquery.suggestions already takes care of this (it never calls
  fetch if the string is empty, and if it did, then this code would've
  left it unhandled causing bugs (e.g. suggestions of the previous
  fetch remain visible).
* Remove redundant promise.abort check.
* Use $.data() and $.removeData() to bypass unneeded convenience
  logic for collections and HTML data attributes.

Change-Id: I59ae21c4b481ef52e4db14cb1f3f2fe026c23b3f

resources/src/mediawiki/mediawiki.searchSuggest.js

index cbdf666..c2c70b0 100644 (file)
@@ -3,7 +3,7 @@
  */
 ( function ( mw, $ ) {
        $( function () {
-               var map, resultRenderCache, searchboxesSelectors,
+               var api, map, resultRenderCache, searchboxesSelectors,
                        // Region where the suggestions box will appear directly below
                        // (using the same width). Can be a container element or the input
                        // itself, depending on what suits best in the environment.
                $( searchboxesSelectors.join( ', ' ) )
                        .suggestions( {
                                fetch: function ( query ) {
-                                       var $el;
-
-                                       if ( query.length !== 0 ) {
-                                               $el = $( this );
-                                               $el.data( 'request', ( new mw.Api() ).get( {
-                                                       action: 'opensearch',
-                                                       search: query,
-                                                       namespace: 0,
-                                                       suggest: ''
-                                               } ).done( function ( data ) {
-                                                       $el.suggestions( 'suggestions', data[1] );
-                                               } ) );
-                                       }
+                                       var $textbox = this,
+                                               node = this[0];
+
+                                       api = api || new mw.Api();
+
+                                       $.data( node, 'request', api.get( {
+                                               action: 'opensearch',
+                                               search: query,
+                                               namespace: 0,
+                                               suggest: ''
+                                       } ).done( function ( data ) {
+                                               $textbox.suggestions( 'suggestions', data[1] );
+                                       } ) );
                                },
                                cancel: function () {
-                                       var apiPromise = $( this ).data( 'request' );
-                                       // If the delay setting has caused the fetch to have not even happened
-                                       // yet, the apiPromise object will have never been set.
-                                       if ( apiPromise && $.isFunction( apiPromise.abort ) ) {
-                                               apiPromise.abort();
-                                               $( this ).removeData( 'request' );
+                                       var node = this[0],
+                                               request = $.data( node, 'request' );
+
+                                       if ( request ) {
+                                               request.abort();
+                                               $.removeData( node, 'request' );
                                        }
                                },
                                result: {
                                        render: renderFunction,
                                        select: function () {
-                                               return true; // allow the form to be submitted
+                                               // allow the form to be submitted
+                                               return true;
                                        }
                                },
                                delay: 120,