From 7af8d6209173b383b9cab99f865ecde729bea00e Mon Sep 17 00:00:00 2001 From: Tomas Kirda Date: Thu, 21 Feb 2013 17:07:33 -0600 Subject: [PATCH] Make query value to be set by the plugin. Fixes #48 --- spec/autocompleteBehavior.js | 35 +++++++++++++++++++++++++++++++++++ src/jquery.autocomplete.js | 6 ++++-- 2 files changed, 39 insertions(+), 2 deletions(-) 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(); }