]> source.dussan.org Git - jquery-ui.git/commitdiff
Autocomplete: Scope race condition handling to the instance. Fixes #9334 - Autocomple...
authorScott González <scott.gonzalez@gmail.com>
Tue, 28 May 2013 15:04:29 +0000 (11:04 -0400)
committerScott González <scott.gonzalez@gmail.com>
Tue, 28 May 2013 15:04:42 +0000 (11:04 -0400)
tests/unit/autocomplete/autocomplete_core.js
ui/jquery.ui.autocomplete.js

index df98725eeb3289e19b6a5ef851d91b43e9d3eaa0..03ebdfec905d57ba13e0622e38ec15610470328e 100644 (file)
@@ -192,6 +192,34 @@ asyncTest( "handle race condition", function() {
        }
 });
 
+asyncTest( "simultaneous searches (#9334)", function() {
+       expect( 2 );
+       var element = $( "#autocomplete" ).autocomplete({
+                       source: function( request, response ) {
+                               setTimeout(function() {
+                                       response([ request.term ]);
+                               });
+                       },
+                       response: function() {
+                               ok( true, "response from first instance" );
+                       }
+               }),
+               element2 = $( "#autocomplete-textarea" ).autocomplete({
+                       source: function( request, response ) {
+                               setTimeout(function() {
+                                       response([ request.term ]);
+                               });
+                       },
+                       response: function() {
+                               ok( true, "response from second instance" );
+                               start();
+                       }
+               });
+
+       element.autocomplete( "search", "test" );
+       element2.autocomplete( "search", "test" );
+});
+
 test( "ARIA", function() {
        expect( 7 );
        var element = $( "#autocomplete" ).autocomplete({
index 6a3e8661d857694b8fefae5c267d4642038274e0..8328cb6e5e7b4ec00f7f59d11cac5aad6b13298d 100644 (file)
@@ -41,6 +41,7 @@ $.widget( "ui.autocomplete", {
                select: null
        },
 
+       requestIndex: 0,
        pending: 0,
 
        _create: function() {
@@ -414,24 +415,20 @@ $.widget( "ui.autocomplete", {
                this.source( { term: value }, this._response() );
        },
 
-       _response: (function() {
-               var requestIndex = 0;
+       _response: function() {
+               var index = ++this.requestIndex;
 
-               return function() {
-                       var index = ++requestIndex;
-
-                       return $.proxy(function( content ) {
-                               if ( index === requestIndex ) {
-                                       this.__response( content );
-                               }
+               return $.proxy(function( content ) {
+                       if ( index === this.requestIndex ) {
+                               this.__response( content );
+                       }
 
-                               this.pending--;
-                               if ( !this.pending ) {
-                                       this.element.removeClass( "ui-autocomplete-loading" );
-                               }
-                       }, this );
-               };
-       })(),
+                       this.pending--;
+                       if ( !this.pending ) {
+                               this.element.removeClass( "ui-autocomplete-loading" );
+                       }
+               }, this );
+       },
 
        __response: function( content ) {
                if ( content ) {