Skip to content

Commit 530d4cb

Browse files
committed
Autocomplete: Cancel search when closing the menu. Fixes #7523 - Autocomplete cancel pending request when text changes below minLength.
1 parent 041cb39 commit 530d4cb

File tree

2 files changed

+40
-1
lines changed

2 files changed

+40
-1
lines changed

tests/unit/autocomplete/autocomplete_options.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,39 @@ test( "minLength", function() {
102102
ok( menu.is( ":visible" ), "blank enough for minLength: 0" );
103103
});
104104

105+
asyncTest( "minLength, exceed then drop below", function() {
106+
expect( 4 );
107+
var element = $( "#autocomplete" ).autocomplete({
108+
minLength: 2,
109+
source: function( req, res ) {
110+
equal( req.term, "12", "correct search term" );
111+
setTimeout(function() {
112+
res([ "item" ]);
113+
}, 1 );
114+
}
115+
}),
116+
menu = element.autocomplete( "widget" );
117+
118+
ok( menu.is( ":hidden" ), "menu is hidden before first search" );
119+
element.autocomplete( "search", "12" );
120+
121+
ok( menu.is( ":hidden" ), "menu is hidden before second search" );
122+
element.autocomplete( "search", "1" );
123+
124+
setTimeout(function() {
125+
ok( menu.is( ":hidden" ), "menu is hidden after searches" );
126+
start();
127+
}, 50 );
128+
});
129+
130+
// TODO: figure out how to implement this test
131+
// When fixing #7523, I couldn't figure out a test that would fail when
132+
// calling .close() (instead of ._close()) from ._response().
133+
// Use the remote demo and type "je", delete, "a", you should get results for "ja"
134+
// but if we call .close() from ._response() the results are ignored.
135+
//asyncTest( "minLength, exceed then drop below", function() {
136+
//});
137+
105138
test( "source, local string array", function() {
106139
expect( 1 );
107140
var element = $( "#autocomplete" ).autocomplete({

ui/jquery.ui.autocomplete.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -383,7 +383,8 @@ $.widget( "ui.autocomplete", {
383383
this._suggest( content );
384384
this._trigger( "open" );
385385
} else {
386-
this.close();
386+
// use ._close() instad of .close() so we don't cancel future searches
387+
this._close();
387388
}
388389
this.pending--;
389390
if ( !this.pending ) {
@@ -392,6 +393,11 @@ $.widget( "ui.autocomplete", {
392393
},
393394

394395
close: function( event ) {
396+
this.cancelSearch = true;
397+
this._close( event );
398+
},
399+
400+
_close: function( event ) {
395401
clearTimeout( this.closing );
396402
if ( this.menu.element.is(":visible") ) {
397403
this.menu.element.hide();

0 commit comments

Comments
 (0)