diff --git a/spec/autocompleteBehavior.js b/spec/autocompleteBehavior.js index 93e1f3a..5a0e595 100644 --- a/spec/autocompleteBehavior.js +++ b/spec/autocompleteBehavior.js @@ -210,6 +210,41 @@ describe('Autocomplete', function () { }); }); + it('Should not require orginal query value from the server', function () { + var input = document.createElement('input'), + ajaxExecuted = false, + url = '/test-original-query', + autocomplete = new $.Autocomplete(input, { + serviceUrl: url + }); + + $.mockjax({ + url: url, + responseTime: 50, + response: function () { + ajaxExecuted = true; + var response = { + query: null, + suggestions: ['A', 'B', 'C'] + }; + this.responseText = JSON.stringify(response); + } + }); + + input.value = 'A'; + autocomplete.onValueChange(); + + waitsFor(function () { + return ajaxExecuted; + }, 'Ajax call never completed.', 100); + + runs(function () { + expect(ajaxExecuted).toBe(true); + expect(autocomplete.suggestions.length).toBe(3); + expect(autocomplete.suggestions[0].value).toBe('A'); + }); + }); + it('Should should not preventDefault when tabDisabled is set to false', function () { var input = document.createElement('input'), autocomplete = new $.Autocomplete(input, { diff --git a/src/jquery.autocomplete.js b/src/jquery.autocomplete.js index f927766..6bc5bb7 100644 --- a/src/jquery.autocomplete.js +++ b/src/jquery.autocomplete.js @@ -95,7 +95,9 @@ }, paramName: 'query', transformResult: function (response, originalQuery) { - return typeof response === 'string' ? $.parseJSON(response) : response; + var result = typeof response === 'string' ? $.parseJSON(response) : response; + result.query = originalQuery; + return result; } }; @@ -491,7 +493,7 @@ } // Display suggestions only if returned query matches current value: - if (result[options.paramName] === that.getQuery(that.currentValue)) { + if (result.query === that.getQuery(that.currentValue)) { that.suggestions = result.suggestions; that.suggest(); }