2
0
mirror of https://github.com/devbridge/jQuery-Autocomplete.git synced 2024-11-25 06:07:45 +00:00

Add flag to disable prevention of AJAX requests for future requests. Closes #122.

This commit is contained in:
Tomas Kirda 2013-11-24 16:01:37 -06:00
parent 4924da0b51
commit e5bbc205df
3 changed files with 60 additions and 2 deletions

View File

@ -36,6 +36,7 @@ The standard jquery.autocomplete.js file is around 2.7KB when minified via Closu
* `onSearchError`: `function (query, jqXHR, textStatus, errorThrown) {}` called if ajax request fails. `this` is bound to input element.
* `onInvalidateSelection`: `function () {}` called when input is altered after selection has been made. `this` is bound to input element.
* `triggerSelectOnValidInput`: Boolean value indicating if `select` should be triggered if it matches suggestion. Default `true`.
* `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`.
* `beforeRender`: `function (container) {}` called before displaying the suggestions. You may manipulate suggestions DOM before it is displayed.
* `tabDisabled`: Default `false`. Set to true to leave the cursor in the input field after the user tabs to select a suggestion.
* `paramName`: Default `query`. The name of the request parameter that contains the query.

View File

@ -597,4 +597,56 @@ describe('Autocomplete', function () {
expect(instance.suggestions.length).toBe(limit);
});
it('Should prevent Ajax requests if previous query with matching root failed.', function () {
var input = $('<input />'),
instance,
serviceUrl = '/autocomplete/prevent/ajax',
ajaxCount = 0;
input.autocomplete({
serviceUrl: serviceUrl
});
$.mockjax({
url: serviceUrl,
responseTime: 5,
response: function (settings) {
ajaxCount++;
var response = { suggestions: [] };
this.responseText = JSON.stringify(response);
}
});
input.val('Jam');
instance = input.autocomplete();
instance.onValueChange();
waits(10);
runs(function (){
expect(ajaxCount).toBe(1);
input.val('Jama');
instance.onValueChange();
});
waits(10);
runs(function (){
// Ajax call should not have bee made:
expect(ajaxCount).toBe(1);
// Change setting and continue:
instance.setOptions({ preventBadQueries: false });
input.val('Jamai');
instance.onValueChange();
});
waits(10);
runs(function (){
// Ajax call should have been made:
expect(ajaxCount).toBe(2);
});
});
});

View File

@ -76,6 +76,7 @@
dataType: 'text',
currentRequest: null,
triggerSelectOnValidInput: true,
preventBadQueries: true,
lookupFilter: function (suggestion, originalQuery, queryLowerCase) {
return suggestion.value.toLowerCase().indexOf(queryLowerCase) !== -1;
},
@ -515,6 +516,10 @@
},
isBadQuery: function (q) {
if (!this.options.preventBadQueries){
return false;
}
var badQueries = this.badQueries,
i = badQueries.length;
@ -647,8 +652,8 @@
// Cache results if cache is not disabled:
if (!options.noCache) {
that.cachedResponse[cacheKey] = result;
if (result.suggestions.length === 0) {
that.badQueries.push(cacheKey);
if (options.preventBadQueries && result.suggestions.length === 0) {
that.badQueries.push(originalQuery);
}
}