From 82eb09328f66e83121be19a248cca413118f31ae Mon Sep 17 00:00:00 2001 From: Jan-Christian Foeh Date: Tue, 1 Oct 2013 15:56:39 +0200 Subject: [PATCH] Add onSearchError callback for handling AJAX failures --- readme.md | 1 + spec/autocompleteBehavior.js | 35 +++++++++++++++++++++++++++++++++++ src/jquery.autocomplete.js | 3 +++ 3 files changed, 39 insertions(+) diff --git a/readme.md b/readme.md index 66447ca..25d428b 100644 --- a/readme.md +++ b/readme.md @@ -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. diff --git a/spec/autocompleteBehavior.js b/spec/autocompleteBehavior.js index a0cb658..c62ca38 100644 --- a/spec/autocompleteBehavior.js +++ b/spec/autocompleteBehavior.js @@ -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, diff --git a/src/jquery.autocomplete.js b/src/jquery.autocomplete.js index 2399079..452a8be 100644 --- a/src/jquery.autocomplete.js +++ b/src/jquery.autocomplete.js @@ -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); }); } },