2
0
mirror of https://github.com/devbridge/jQuery-Autocomplete.git synced 2024-11-22 04:45:12 +00:00

Add onSearchError callback for handling AJAX failures

This commit is contained in:
Jan-Christian Foeh 2013-10-01 15:56:39 +02:00
parent 2f260cec63
commit 82eb09328f
3 changed files with 39 additions and 0 deletions

View File

@ -32,6 +32,7 @@ The standard jquery.autocomplete.js file is around 2.7KB when minified via Closu
* `noCache`: Boolean value indicating whether to cache suggestion results. Default `false`.
* `onSearchStart`: `function (query) {}` called before ajax request. `this` is bound to input element.
* `onSearchComplete`: `function (query) {}` called after ajax response is processed. `this` is bound to input element.
* `onSearchError`: `function (query, jqXHR, textStatus, errorThrown) {}` called if ajax request fails. `this` is bound to input element.
* `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.
* `transformResult`: `function(response, originalQuery) {}` called after the result of the query is ready. Converts the result into response.suggestions format.

View File

@ -171,6 +171,41 @@ describe('Autocomplete', function () {
});
});
it('Should execute onSearchError', function () {
var input = document.createElement('input'),
ajaxExecuted = false,
errorMessage = false,
url = '/test-error',
autocomplete = new $.Autocomplete(input, {
serviceUrl: url,
onSearchError: function (q, jqXHR, textStatus, errorThrown) {
errorMessage = jqXHR.responseText;
}
});
$.mockjax({
url: url,
responseTime: 50,
status: 500,
response: function (settings) {
ajaxExecuted = true;
this.responseText = "An error occurred";
}
});
input.value = 'A';
autocomplete.onValueChange();
waitsFor(function () {
return ajaxExecuted;
}, 'Ajax call never completed.', 100);
runs(function () {
expect(ajaxExecuted).toBe(true);
expect(errorMessage).toBe("An error occurred");
});
});
it('Should transform results', function () {
var input = document.createElement('input'),
ajaxExecuted = false,

View File

@ -68,6 +68,7 @@
noCache: false,
onSearchStart: noop,
onSearchComplete: noop,
onSearchError: noop,
containerClass: 'autocomplete-suggestions',
tabDisabled: false,
dataType: 'text',
@ -441,6 +442,8 @@
}).done(function (data) {
that.processResponse(data, q);
options.onSearchComplete.call(that.element, q);
}).fail(function (jqXHR, textStatus, errorThrown) {
options.onSearchError.call(that.element, q, jqXHR, textStatus, errorThrown);
});
}
},