From 0b7f00dad0263ec6e3690e4f73ac825d3e50c163 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mario=20Andre=CC=81s=20Correa?= Date: Sat, 4 Jan 2014 01:45:07 -0500 Subject: [PATCH] `onSearchComplete` receives suggestions as second parameter MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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`. --- spec/autocompleteBehavior.js | 11 ++++++++--- src/jquery.autocomplete.js | 11 ++++++----- 2 files changed, 14 insertions(+), 8 deletions(-) diff --git a/spec/autocompleteBehavior.js b/spec/autocompleteBehavior.js index d8be70e..80718cf 100644 --- a/spec/autocompleteBehavior.js +++ b/spec/autocompleteBehavior.js @@ -136,12 +136,15 @@ describe('Autocomplete', function () { it('Should execute onSearchComplete', function () { var input = document.createElement('input'), completeQuery, + mockupSuggestion = { value: 'A', data: 'A' }, + resultSuggestions, ajaxExecuted = false, url = '/test-completed', autocomplete = new $.Autocomplete(input, { serviceUrl: url, - onSearchComplete: function (query) { + onSearchComplete: function (query, suggestions) { completeQuery = query; + resultSuggestions = suggestions; } }); @@ -153,7 +156,7 @@ describe('Autocomplete', function () { var query = settings.data.query, response = { query: query, - suggestions: [] + suggestions: [mockupSuggestion] }; this.responseText = JSON.stringify(response); } @@ -169,6 +172,8 @@ describe('Autocomplete', function () { runs(function () { expect(ajaxExecuted).toBe(true); 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); }); }); -}); \ No newline at end of file +}); diff --git a/src/jquery.autocomplete.js b/src/jquery.autocomplete.js index 68b32c2..f2114bf 100644 --- a/src/jquery.autocomplete.js +++ b/src/jquery.autocomplete.js @@ -506,9 +506,11 @@ type: options.type, dataType: options.dataType }).done(function (data) { + var result; that.currentRequest = null; - that.processResponse(data, q, cacheKey); - options.onSearchComplete.call(that.element, q); + result = options.transformResult(data); + that.processResponse(result, q, cacheKey); + options.onSearchComplete.call(that.element, q, result.suggestions); }).fail(function (jqXHR, textStatus, errorThrown) { options.onSearchError.call(that.element, q, jqXHR, textStatus, errorThrown); }); @@ -642,10 +644,9 @@ return suggestions; }, - processResponse: function (response, originalQuery, cacheKey) { + processResponse: function (result, originalQuery, cacheKey) { var that = this, - options = that.options, - result = options.transformResult(response, originalQuery); + options = that.options; result.suggestions = that.verifySuggestionsFormat(result.suggestions);