]> source.dussan.org Git - jquery-ui.git/commitdiff
Autocomplete: Move race condition logic from ajax requests to general response handle...
authorScott González <scott.gonzalez@gmail.com>
Tue, 3 Apr 2012 20:47:14 +0000 (16:47 -0400)
committerScott González <scott.gonzalez@gmail.com>
Tue, 3 Apr 2012 20:47:14 +0000 (16:47 -0400)
tests/unit/autocomplete/autocomplete_core.js
ui/jquery.ui.autocomplete.js

index ef6ea58afe1a7893c2ba445e148043e0651e0cd6..daeea09720b78b9907afe0ea9f34a466c4aa2fdb 100644 (file)
@@ -123,4 +123,33 @@ test( "allow form submit on enter when menu is not active", function() {
        }
 })();
 
+asyncTest( "handle race condition", function() {
+       expect( 3 );
+       var count = 0,
+               element = $( "#autocomplete" ).autocomplete({
+               source: function( request, response ) {
+                       count++;
+                       if ( request.term.length === 1 ) {
+                               equal( count, 1, "request with 1 character is first" );
+                               setTimeout(function() {
+                                       response([ "one" ]);
+                                       setTimeout( checkResults, 1 );
+                               }, 1 );
+                               return;
+                       }
+                       equal( count, 2, "request with 2 characters is second" );
+                       response([ "two" ]);
+               }
+       });
+
+       element.autocomplete( "search", "a" );
+       element.autocomplete( "search", "ab" );
+
+       function checkResults() {
+               equal( element.autocomplete( "widget" ).find( ".ui-menu-item" ).text(), "two",
+                       "correct results displayed" );
+               start();
+       }
+});
+
 }( jQuery ) );
index 7051c90af9e9fa7b610a5c587a7ac784bd00bbe7..226d406053eeced3928e78113696afaa14a70903 100644 (file)
@@ -186,9 +186,6 @@ $.widget( "ui.autocomplete", {
                                self._change( event );
                        });
                this._initSource();
-               this.response = function() {
-                       return self._response.apply( self, arguments );
-               };
                this.menu = $( "<ul></ul>" )
                        .addClass( "ui-autocomplete" )
                        .appendTo( this.document.find( this.options.appendTo || "body" )[0] )
@@ -326,18 +323,11 @@ $.widget( "ui.autocomplete", {
                                        url: url,
                                        data: request,
                                        dataType: "json",
-                                       context: {
-                                               autocompleteRequest: ++requestIndex
-                                       },
                                        success: function( data, status ) {
-                                               if ( this.autocompleteRequest === requestIndex ) {
-                                                       response( data );
-                                               }
+                                               response( data );
                                        },
                                        error: function() {
-                                               if ( this.autocompleteRequest === requestIndex ) {
-                                                       response( [] );
-                                               }
+                                               response( [] );
                                        }
                                });
                        };
@@ -380,10 +370,26 @@ $.widget( "ui.autocomplete", {
                this.element.addClass( "ui-autocomplete-loading" );
                this.cancelSearch = false;
 
-               this.source( { term: value }, this.response );
+               this.source( { term: value }, this._response() );
+       },
+
+       _response: function() {
+               var that = this,
+                       index = ++requestIndex;
+
+               return function( content ) {
+                       if ( index === requestIndex ) {
+                               that.__response( content );
+                       }
+
+                       that.pending--;
+                       if ( !that.pending ) {
+                               that.element.removeClass( "ui-autocomplete-loading" );
+                       }
+               };
        },
 
-       _response: function( content ) {
+       __response: function( content ) {
                if ( content ) {
                        content = this._normalize( content );
                }
@@ -395,10 +401,6 @@ $.widget( "ui.autocomplete", {
                        // use ._close() instead of .close() so we don't cancel future searches
                        this._close();
                }
-               this.pending--;
-               if ( !this.pending ) {
-                       this.element.removeClass( "ui-autocomplete-loading" );
-               }
        },
 
        close: function( event ) {