Autocomplete: Scope race condition handling to the instance. Fixes #9334 - Autocomplete: Multiple instances should be able to start asynchronous requests simultaneously.

(cherry picked from commit 9e00e00f3b)
This commit is contained in:
Scott González 2013-05-28 11:04:29 -04:00
parent 26238369bb
commit e4a194ce42
2 changed files with 41 additions and 16 deletions

View 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({

View File

@ -41,6 +41,7 @@ $.widget( "ui.autocomplete", {
select: null
},
requestIndex: 0,
pending: 0,
_create: function() {
@ -415,24 +416,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 === this.requestIndex ) {
this.__response( content );
}
return $.proxy(function( content ) {
if ( index === 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 ) {