Skip to content

Commit 7af8d62

Browse files
author
Tomas Kirda
committed
Make query value to be set by the plugin. Fixes devbridge#48
1 parent a362ce1 commit 7af8d62

File tree

2 files changed

+39
-2
lines changed

2 files changed

+39
-2
lines changed

spec/autocompleteBehavior.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,41 @@ describe('Autocomplete', function () {
210210
});
211211
});
212212

213+
it('Should not require orginal query value from the server', function () {
214+
var input = document.createElement('input'),
215+
ajaxExecuted = false,
216+
url = '/test-original-query',
217+
autocomplete = new $.Autocomplete(input, {
218+
serviceUrl: url
219+
});
220+
221+
$.mockjax({
222+
url: url,
223+
responseTime: 50,
224+
response: function () {
225+
ajaxExecuted = true;
226+
var response = {
227+
query: null,
228+
suggestions: ['A', 'B', 'C']
229+
};
230+
this.responseText = JSON.stringify(response);
231+
}
232+
});
233+
234+
input.value = 'A';
235+
autocomplete.onValueChange();
236+
237+
waitsFor(function () {
238+
return ajaxExecuted;
239+
}, 'Ajax call never completed.', 100);
240+
241+
runs(function () {
242+
expect(ajaxExecuted).toBe(true);
243+
expect(autocomplete.suggestions.length).toBe(3);
244+
expect(autocomplete.suggestions[0].value).toBe('A');
245+
});
246+
});
247+
213248
it('Should should not preventDefault when tabDisabled is set to false', function () {
214249
var input = document.createElement('input'),
215250
autocomplete = new $.Autocomplete(input, {

src/jquery.autocomplete.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,9 @@
9595
},
9696
paramName: 'query',
9797
transformResult: function (response, originalQuery) {
98-
return typeof response === 'string' ? $.parseJSON(response) : response;
98+
var result = typeof response === 'string' ? $.parseJSON(response) : response;
99+
result.query = originalQuery;
100+
return result;
99101
}
100102
};
101103

@@ -491,7 +493,7 @@
491493
}
492494

493495
// Display suggestions only if returned query matches current value:
494-
if (result[options.paramName] === that.getQuery(that.currentValue)) {
496+
if (result.query === that.getQuery(that.currentValue)) {
495497
that.suggestions = result.suggestions;
496498
that.suggest();
497499
}

0 commit comments

Comments
 (0)