Skip to content

Commit f7716b6

Browse files
author
Tomas Kirda
committed
Add beforeRender callback. Closes devbridge#84
1 parent 684bf9e commit f7716b6

File tree

3 files changed

+33
-2
lines changed

3 files changed

+33
-2
lines changed

readme.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ The standard jquery.autocomplete.js file is around 2.7KB when minified via Closu
3232
* `noCache`: Boolean value indicating whether to cache suggestion results. Default `false`.
3333
* `onSearchStart`: `function (query) {}` called before ajax request. `this` is bound to input element.
3434
* `onSearchComplete`: `function (query) {}` called after ajax response is processed. `this` is bound to input element.
35+
* `beforeRender`: `function (container) {}` called before displaying the suggestions. You may manipulate suggestions DOM before it is displayed.
3536
* `tabDisabled`: Default `false`. Set to true to leave the cursor in the input field after the user tabs to select a suggestion.
3637
* `paramName`: Default `query`. The name of the request parameter that contains the query.
3738
* `transformResult`: `function(response, originalQuery) {}` called after the result of the query is ready. Converts the result into response.suggestions format.

spec/autocompleteBehavior.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -438,4 +438,27 @@ describe('Autocomplete', function () {
438438

439439
expect(width).toBeGreaterThan(0);
440440
});
441+
442+
it('Should call beforeRender and pass container jQuery object', function () {
443+
var element = document.createElement('input'),
444+
input = $(element),
445+
instance,
446+
elementCount,
447+
context;
448+
449+
input.autocomplete({
450+
lookup: [{ value: 'Jamaica', data: 'B' }],
451+
beforeRender: function (container) {
452+
context = this;
453+
elementCount = container.length;
454+
}
455+
});
456+
457+
input.val('Jam');
458+
instance = input.autocomplete();
459+
instance.onValueChange();
460+
461+
expect(context).toBe(element);
462+
expect(elementCount).toBe(1);
463+
});
441464
});

src/jquery.autocomplete.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -500,6 +500,7 @@
500500
className = that.classes.suggestion,
501501
classSelected = that.classes.selected,
502502
container = $(that.suggestionsContainer),
503+
beforeRender = that.options.beforeRender,
503504
html = '',
504505
width;
505506

@@ -517,15 +518,21 @@
517518
container.width(width > 0 ? width : 300);
518519
}
519520

520-
container.html(html).show();
521-
that.visible = true;
521+
container.html(html);
522522

523523
// Select first value by default:
524524
if (that.options.autoSelectFirst) {
525525
that.selectedIndex = 0;
526526
container.children().first().addClass(classSelected);
527527
}
528528

529+
if ($.isFunction(beforeRender)) {
530+
beforeRender.call(that.element, container);
531+
}
532+
533+
container.show();
534+
that.visible = true;
535+
529536
that.findBestHint();
530537
},
531538

0 commit comments

Comments
 (0)