Skip to content

Commit 0b7f00d

Browse files
committed
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`.
1 parent 0ee2ba5 commit 0b7f00d

File tree

2 files changed

+14
-8
lines changed

2 files changed

+14
-8
lines changed

spec/autocompleteBehavior.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -136,12 +136,15 @@ describe('Autocomplete', function () {
136136
it('Should execute onSearchComplete', function () {
137137
var input = document.createElement('input'),
138138
completeQuery,
139+
mockupSuggestion = { value: 'A', data: 'A' },
140+
resultSuggestions,
139141
ajaxExecuted = false,
140142
url = '/test-completed',
141143
autocomplete = new $.Autocomplete(input, {
142144
serviceUrl: url,
143-
onSearchComplete: function (query) {
145+
onSearchComplete: function (query, suggestions) {
144146
completeQuery = query;
147+
resultSuggestions = suggestions;
145148
}
146149
});
147150

@@ -153,7 +156,7 @@ describe('Autocomplete', function () {
153156
var query = settings.data.query,
154157
response = {
155158
query: query,
156-
suggestions: []
159+
suggestions: [mockupSuggestion]
157160
};
158161
this.responseText = JSON.stringify(response);
159162
}
@@ -169,6 +172,8 @@ describe('Autocomplete', function () {
169172
runs(function () {
170173
expect(ajaxExecuted).toBe(true);
171174
expect(completeQuery).toBe('A');
175+
expect(resultSuggestions[0].value).toBe('A');
176+
expect(resultSuggestions[0].data).toBe('A');
172177
});
173178
});
174179

@@ -649,4 +654,4 @@ describe('Autocomplete', function () {
649654
expect(ajaxCount).toBe(2);
650655
});
651656
});
652-
});
657+
});

src/jquery.autocomplete.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -506,9 +506,11 @@
506506
type: options.type,
507507
dataType: options.dataType
508508
}).done(function (data) {
509+
var result;
509510
that.currentRequest = null;
510-
that.processResponse(data, q, cacheKey);
511-
options.onSearchComplete.call(that.element, q);
511+
result = options.transformResult(data);
512+
that.processResponse(result, q, cacheKey);
513+
options.onSearchComplete.call(that.element, q, result.suggestions);
512514
}).fail(function (jqXHR, textStatus, errorThrown) {
513515
options.onSearchError.call(that.element, q, jqXHR, textStatus, errorThrown);
514516
});
@@ -642,10 +644,9 @@
642644
return suggestions;
643645
},
644646

645-
processResponse: function (response, originalQuery, cacheKey) {
647+
processResponse: function (result, originalQuery, cacheKey) {
646648
var that = this,
647-
options = that.options,
648-
result = options.transformResult(response, originalQuery);
649+
options = that.options;
649650

650651
result.suggestions = that.verifySuggestionsFormat(result.suggestions);
651652

0 commit comments

Comments
 (0)