2
0
mirror of https://github.com/devbridge/jQuery-Autocomplete.git synced 2025-01-23 15:18:28 +00:00

onSearchComplete receives suggestions as second parameter

Added functionality and it’s specs.
In line 648 there is no need to serialise `result` as it will be
already serialised on line `511`.
This commit is contained in:
Mario Andrés Correa 2014-01-04 01:45:07 -05:00
parent 0ee2ba58dc
commit 0b7f00dad0
2 changed files with 14 additions and 8 deletions

View File

@ -136,12 +136,15 @@ describe('Autocomplete', function () {
it('Should execute onSearchComplete', function () { it('Should execute onSearchComplete', function () {
var input = document.createElement('input'), var input = document.createElement('input'),
completeQuery, completeQuery,
mockupSuggestion = { value: 'A', data: 'A' },
resultSuggestions,
ajaxExecuted = false, ajaxExecuted = false,
url = '/test-completed', url = '/test-completed',
autocomplete = new $.Autocomplete(input, { autocomplete = new $.Autocomplete(input, {
serviceUrl: url, serviceUrl: url,
onSearchComplete: function (query) { onSearchComplete: function (query, suggestions) {
completeQuery = query; completeQuery = query;
resultSuggestions = suggestions;
} }
}); });
@ -153,7 +156,7 @@ describe('Autocomplete', function () {
var query = settings.data.query, var query = settings.data.query,
response = { response = {
query: query, query: query,
suggestions: [] suggestions: [mockupSuggestion]
}; };
this.responseText = JSON.stringify(response); this.responseText = JSON.stringify(response);
} }
@ -169,6 +172,8 @@ describe('Autocomplete', function () {
runs(function () { runs(function () {
expect(ajaxExecuted).toBe(true); expect(ajaxExecuted).toBe(true);
expect(completeQuery).toBe('A'); expect(completeQuery).toBe('A');
expect(resultSuggestions[0].value).toBe('A');
expect(resultSuggestions[0].data).toBe('A');
}); });
}); });
@ -649,4 +654,4 @@ describe('Autocomplete', function () {
expect(ajaxCount).toBe(2); expect(ajaxCount).toBe(2);
}); });
}); });
}); });

View File

@ -506,9 +506,11 @@
type: options.type, type: options.type,
dataType: options.dataType dataType: options.dataType
}).done(function (data) { }).done(function (data) {
var result;
that.currentRequest = null; that.currentRequest = null;
that.processResponse(data, q, cacheKey); result = options.transformResult(data);
options.onSearchComplete.call(that.element, q); that.processResponse(result, q, cacheKey);
options.onSearchComplete.call(that.element, q, result.suggestions);
}).fail(function (jqXHR, textStatus, errorThrown) { }).fail(function (jqXHR, textStatus, errorThrown) {
options.onSearchError.call(that.element, q, jqXHR, textStatus, errorThrown); options.onSearchError.call(that.element, q, jqXHR, textStatus, errorThrown);
}); });
@ -642,10 +644,9 @@
return suggestions; return suggestions;
}, },
processResponse: function (response, originalQuery, cacheKey) { processResponse: function (result, originalQuery, cacheKey) {
var that = this, var that = this,
options = that.options, options = that.options;
result = options.transformResult(response, originalQuery);
result.suggestions = that.verifySuggestionsFormat(result.suggestions); result.suggestions = that.verifySuggestionsFormat(result.suggestions);