Skip to content

Commit 82eb093

Browse files
committed
Add onSearchError callback for handling AJAX failures
1 parent 2f260ce commit 82eb093

File tree

3 files changed

+39
-0
lines changed

3 files changed

+39
-0
lines changed

readme.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ The standard jquery.autocomplete.js file is around 2.7KB when minified via Closu
3232
* `noCache`: Boolean value indicating whether to cache suggestion results. Default `false`.
3333
* `onSearchStart`: `function (query) {}` called before ajax request. `this` is bound to input element.
3434
* `onSearchComplete`: `function (query) {}` called after ajax response is processed. `this` is bound to input element.
35+
* `onSearchError`: `function (query, jqXHR, textStatus, errorThrown) {}` called if ajax request fails. `this` is bound to input element.
3536
* `tabDisabled`: Default `false`. Set to true to leave the cursor in the input field after the user tabs to select a suggestion.
3637
* `paramName`: Default `query`. The name of the request parameter that contains the query.
3738
* `transformResult`: `function(response, originalQuery) {}` called after the result of the query is ready. Converts the result into response.suggestions format.

spec/autocompleteBehavior.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,41 @@ describe('Autocomplete', function () {
171171
});
172172
});
173173

174+
it('Should execute onSearchError', function () {
175+
var input = document.createElement('input'),
176+
ajaxExecuted = false,
177+
errorMessage = false,
178+
url = '/test-error',
179+
autocomplete = new $.Autocomplete(input, {
180+
serviceUrl: url,
181+
onSearchError: function (q, jqXHR, textStatus, errorThrown) {
182+
errorMessage = jqXHR.responseText;
183+
}
184+
});
185+
186+
$.mockjax({
187+
url: url,
188+
responseTime: 50,
189+
status: 500,
190+
response: function (settings) {
191+
ajaxExecuted = true;
192+
this.responseText = "An error occurred";
193+
}
194+
});
195+
196+
input.value = 'A';
197+
autocomplete.onValueChange();
198+
199+
waitsFor(function () {
200+
return ajaxExecuted;
201+
}, 'Ajax call never completed.', 100);
202+
203+
runs(function () {
204+
expect(ajaxExecuted).toBe(true);
205+
expect(errorMessage).toBe("An error occurred");
206+
});
207+
});
208+
174209
it('Should transform results', function () {
175210
var input = document.createElement('input'),
176211
ajaxExecuted = false,

src/jquery.autocomplete.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@
6868
noCache: false,
6969
onSearchStart: noop,
7070
onSearchComplete: noop,
71+
onSearchError: noop,
7172
containerClass: 'autocomplete-suggestions',
7273
tabDisabled: false,
7374
dataType: 'text',
@@ -441,6 +442,8 @@
441442
}).done(function (data) {
442443
that.processResponse(data, q);
443444
options.onSearchComplete.call(that.element, q);
445+
}).fail(function (jqXHR, textStatus, errorThrown) {
446+
options.onSearchError.call(that.element, q, jqXHR, textStatus, errorThrown);
444447
});
445448
}
446449
},

0 commit comments

Comments
 (0)