Skip to content

Commit

Permalink
Make query value to be set by the plugin. Fixes devbridge#48
Browse files Browse the repository at this point in the history
  • Loading branch information
Tomas Kirda committed Feb 21, 2013
1 parent a362ce1 commit 7af8d62
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 2 deletions.
35 changes: 35 additions & 0 deletions spec/autocompleteBehavior.js
Original file line number Diff line number Diff line change
Expand Up @@ -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, {
Expand Down
6 changes: 4 additions & 2 deletions src/jquery.autocomplete.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
};

Expand Down Expand Up @@ -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();
}
Expand Down

0 comments on commit 7af8d62

Please sign in to comment.