Permalink
Browse files

Autocomplete: Scope race condition handling to the instance. Fixes #9…

…334 - Autocomplete: Multiple instances should be able to start asynchronous requests simultaneously.
  • Loading branch information...
scottgonzalez committed May 28, 2013
1 parent d65cc93 commit 9e00e00f3b54770faa0291d6ee6fc1dcbad028cb
Showing with 41 additions and 16 deletions.
  1. +28 −0 tests/unit/autocomplete/autocomplete_core.js
  2. +13 −16 ui/jquery.ui.autocomplete.js
@@ -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({
@@ -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 ) {

0 comments on commit 9e00e00

Please sign in to comment.