mediawiki.searchSuggest: Use mw.Api instead of jQuery.ajax
authorAlex Monk <krenair@gmail.com>
Sun, 26 May 2013 18:46:04 +0000 (19:46 +0100)
committerAlex Monk <krenair@gmail.com>
Tue, 28 May 2013 11:36:49 +0000 (12:36 +0100)
Change-Id: I5e12371bf7e0a6eaeb2507b8942773f63281139b

resources/Resources.php
resources/mediawiki/mediawiki.searchSuggest.js

index ddcefda..7361b72 100644 (file)
@@ -668,6 +668,7 @@ return array(
                        'jquery.client',
                        'jquery.placeholder',
                        'jquery.suggestions',
+                       'mediawiki.api',
                ),
        ),
        'mediawiki.Title' => array(
index 00e74c5..08f10fe 100644 (file)
                $( searchboxesSelectors.join(', ') )
                        .suggestions( {
                                fetch: function ( query ) {
-                                       var $el, jqXhr;
+                                       var $el;
 
                                        if ( query.length !== 0 ) {
-                                               $el = $(this);
-                                               jqXhr = $.ajax( {
-                                                       url: mw.util.wikiScript( 'api' ),
-                                                       data: {
-                                                               format: 'json',
-                                                               action: 'opensearch',
-                                                               search: query,
-                                                               namespace: 0,
-                                                               suggest: ''
-                                                       },
-                                                       dataType: 'json',
-                                                       success: function ( data ) {
-                                                               if ( $.isArray( data ) && data.length ) {
-                                                                       $el.suggestions( 'suggestions', data[1] );
-                                                               }
-                                                       }
-                                               });
-                                               $el.data( 'request', jqXhr );
+                                               $el = $( this );
+                                               $el.data( 'request', ( new mw.Api() ).get( {
+                                                       action: 'opensearch',
+                                                       search: query,
+                                                       namespace: 0,
+                                                       suggest: ''
+                                               } ).done( function ( data ) {
+                                                       $el.suggestions( 'suggestions', data[1] );
+                                               } ) );
                                        }
                                },
                                cancel: function () {
-                                       var jqXhr = $(this).data( 'request' );
+                                       var apiPromise = $( this ).data( 'request' );
                                        // If the delay setting has caused the fetch to have not even happened
-                                       // yet, the jqXHR object will have never been set.
-                                       if ( jqXhr && $.isFunction( jqXhr.abort ) ) {
-                                               jqXhr.abort();
-                                               $(this).removeData( 'request' );
+                                       // yet, the apiPromise object will have never been set.
+                                       if ( apiPromise && $.isFunction( apiPromise.abort ) ) {
+                                               apiPromise.abort();
+                                               $( this ).removeData( 'request' );
                                        }
                                },
                                result: {