From f7716b655117a82b0d05f379237a27cf45b89227 Mon Sep 17 00:00:00 2001 From: Tomas Kirda Date: Sat, 31 Aug 2013 17:13:27 -0500 Subject: [PATCH] Add beforeRender callback. Closes #84 --- readme.md | 1 + spec/autocompleteBehavior.js | 23 +++++++++++++++++++++++ src/jquery.autocomplete.js | 11 +++++++++-- 3 files changed, 33 insertions(+), 2 deletions(-) diff --git a/readme.md b/readme.md index 66447ca..a1c50cc 100644 --- a/readme.md +++ b/readme.md @@ -32,6 +32,7 @@ The standard jquery.autocomplete.js file is around 2.7KB when minified via Closu * `noCache`: Boolean value indicating whether to cache suggestion results. Default `false`. * `onSearchStart`: `function (query) {}` called before ajax request. `this` is bound to input element. * `onSearchComplete`: `function (query) {}` called after ajax response is processed. `this` is bound to input element. + * `beforeRender`: `function (container) {}` called before displaying the suggestions. You may manipulate suggestions DOM before it is displayed. * `tabDisabled`: Default `false`. Set to true to leave the cursor in the input field after the user tabs to select a suggestion. * `paramName`: Default `query`. The name of the request parameter that contains the query. * `transformResult`: `function(response, originalQuery) {}` called after the result of the query is ready. Converts the result into response.suggestions format. diff --git a/spec/autocompleteBehavior.js b/spec/autocompleteBehavior.js index a0cb658..5e64c7b 100644 --- a/spec/autocompleteBehavior.js +++ b/spec/autocompleteBehavior.js @@ -438,4 +438,27 @@ describe('Autocomplete', function () { expect(width).toBeGreaterThan(0); }); + + it('Should call beforeRender and pass container jQuery object', function () { + var element = document.createElement('input'), + input = $(element), + instance, + elementCount, + context; + + input.autocomplete({ + lookup: [{ value: 'Jamaica', data: 'B' }], + beforeRender: function (container) { + context = this; + elementCount = container.length; + } + }); + + input.val('Jam'); + instance = input.autocomplete(); + instance.onValueChange(); + + expect(context).toBe(element); + expect(elementCount).toBe(1); + }); }); \ No newline at end of file diff --git a/src/jquery.autocomplete.js b/src/jquery.autocomplete.js index 918eee4..c7369e3 100644 --- a/src/jquery.autocomplete.js +++ b/src/jquery.autocomplete.js @@ -500,6 +500,7 @@ className = that.classes.suggestion, classSelected = that.classes.selected, container = $(that.suggestionsContainer), + beforeRender = that.options.beforeRender, html = '', width; @@ -517,8 +518,7 @@ container.width(width > 0 ? width : 300); } - container.html(html).show(); - that.visible = true; + container.html(html); // Select first value by default: if (that.options.autoSelectFirst) { @@ -526,6 +526,13 @@ container.children().first().addClass(classSelected); } + if ($.isFunction(beforeRender)) { + beforeRender.call(that.element, container); + } + + container.show(); + that.visible = true; + that.findBestHint(); },