Skip to content

Commit e5bbc20

Browse files
author
Tomas Kirda
committed
Add flag to disable prevention of AJAX requests for future requests. Closes #122.
1 parent 4924da0 commit e5bbc20

File tree

3 files changed

+60
-2
lines changed

3 files changed

+60
-2
lines changed

readme.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ The standard jquery.autocomplete.js file is around 2.7KB when minified via Closu
3636
* `onSearchError`: `function (query, jqXHR, textStatus, errorThrown) {}` called if ajax request fails. `this` is bound to input element.
3737
* `onInvalidateSelection`: `function () {}` called when input is altered after selection has been made. `this` is bound to input element.
3838
* `triggerSelectOnValidInput`: Boolean value indicating if `select` should be triggered if it matches suggestion. Default `true`.
39+
* `preventBadQueries`: Boolean value indicating if it shoud prevent future ajax requests for queries with the same root if no results were returned. E.g. if `Jam` returns no suggestions, it will not fire for any future query that starts with `Jam`. Default `true`.
3940
* `beforeRender`: `function (container) {}` called before displaying the suggestions. You may manipulate suggestions DOM before it is displayed.
4041
* `tabDisabled`: Default `false`. Set to true to leave the cursor in the input field after the user tabs to select a suggestion.
4142
* `paramName`: Default `query`. The name of the request parameter that contains the query.

spec/autocompleteBehavior.js

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -597,4 +597,56 @@ describe('Autocomplete', function () {
597597

598598
expect(instance.suggestions.length).toBe(limit);
599599
});
600+
601+
it('Should prevent Ajax requests if previous query with matching root failed.', function () {
602+
var input = $('<input />'),
603+
instance,
604+
serviceUrl = '/autocomplete/prevent/ajax',
605+
ajaxCount = 0;
606+
607+
input.autocomplete({
608+
serviceUrl: serviceUrl
609+
});
610+
611+
$.mockjax({
612+
url: serviceUrl,
613+
responseTime: 5,
614+
response: function (settings) {
615+
ajaxCount++;
616+
var response = { suggestions: [] };
617+
this.responseText = JSON.stringify(response);
618+
}
619+
});
620+
621+
input.val('Jam');
622+
instance = input.autocomplete();
623+
instance.onValueChange();
624+
625+
waits(10);
626+
627+
runs(function (){
628+
expect(ajaxCount).toBe(1);
629+
input.val('Jama');
630+
instance.onValueChange();
631+
});
632+
633+
waits(10);
634+
635+
runs(function (){
636+
// Ajax call should not have bee made:
637+
expect(ajaxCount).toBe(1);
638+
639+
// Change setting and continue:
640+
instance.setOptions({ preventBadQueries: false });
641+
input.val('Jamai');
642+
instance.onValueChange();
643+
});
644+
645+
waits(10);
646+
647+
runs(function (){
648+
// Ajax call should have been made:
649+
expect(ajaxCount).toBe(2);
650+
});
651+
});
600652
});

src/jquery.autocomplete.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@
7676
dataType: 'text',
7777
currentRequest: null,
7878
triggerSelectOnValidInput: true,
79+
preventBadQueries: true,
7980
lookupFilter: function (suggestion, originalQuery, queryLowerCase) {
8081
return suggestion.value.toLowerCase().indexOf(queryLowerCase) !== -1;
8182
},
@@ -515,6 +516,10 @@
515516
},
516517

517518
isBadQuery: function (q) {
519+
if (!this.options.preventBadQueries){
520+
return false;
521+
}
522+
518523
var badQueries = this.badQueries,
519524
i = badQueries.length;
520525

@@ -647,8 +652,8 @@
647652
// Cache results if cache is not disabled:
648653
if (!options.noCache) {
649654
that.cachedResponse[cacheKey] = result;
650-
if (result.suggestions.length === 0) {
651-
that.badQueries.push(cacheKey);
655+
if (options.preventBadQueries && result.suggestions.length === 0) {
656+
that.badQueries.push(originalQuery);
652657
}
653658
}
654659

0 commit comments

Comments
 (0)