Skip to content

Commit a2c4728

Browse files
author
Tomas Kirda
committed
Merge branch 'onhide-callback-option' of https://github.com/stffndtz/jQuery-Autocomplete into stffndtz-onhide-callback-option
Conflicts: dist/jquery.autocomplete.js dist/jquery.autocomplete.min.js readme.md spec/autocompleteBehavior.js src/jquery.autocomplete.js
2 parents c9d106b + d9b0033 commit a2c4728

File tree

3 files changed

+47
-12
lines changed

3 files changed

+47
-12
lines changed

readme.md

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#Ajax AutoComplete for jQuery
22

3-
Ajax Autocomplete for jQuery allows you to easily create
3+
Ajax Autocomplete for jQuery allows you to easily create
44
autocomplete/autosuggest boxes for text input fields.
55

66
Has no dependencies other than jQuery.
@@ -18,15 +18,15 @@ The standard jquery.autocomplete.js file is around 2.7KB when minified via Closu
1818
* `suggestion`: An object literal with the following format: `{ value: 'string', data: any }`.
1919
* `lookupFilter`: `function (suggestion, query, queryLowerCase) {}` filter function for local lookups. By default it does partial string match (case insensitive).
2020
* `lookupLimit`: Number of maximum results to display for local lookup. Default: no limit.
21-
* `onSelect`: `function (suggestion) {}` Callback function invoked when user selects suggestion
21+
* `onSelect`: `function (suggestion) {}` Callback function invoked when user selects suggestion
2222
from the list. `this` inside callback refers to input HtmlElement.
2323
* `minChars`: Minimum number of characters required to trigger autosuggest. Default: `1`.
2424
* `maxHeight`: Maximum height of the suggestions container in pixels. Default: `300`.
2525
* `deferRequestBy`: Number of miliseconds to defer ajax request. Default: `0`.
2626
* `width`: Suggestions container width in pixels, e.g.: 300. Default: `auto`, takes input field width.
2727
* `params`: Additional parameters to pass with the request, optional.
28-
* `formatResult`: `function (suggestion, currentValue) {}` custom function to
29-
format suggestion entry inside suggestions container, optional.
28+
* `formatResult`: `function (suggestion, currentValue) {}` custom function to
29+
format suggestion entry inside suggestions container, optional.
3030
* `delimiter`: String or RegExp, that splits input value and takes last part to as query for suggestions.
3131
Useful when for example you need to fill list of coma separated values.
3232
* `zIndex`: 'z-index' for suggestions container. Default: `9999`.
@@ -37,7 +37,7 @@ The standard jquery.autocomplete.js file is around 2.7KB when minified via Closu
3737
* `onSearchError`: `function (query, jqXHR, textStatus, errorThrown) {}` called if ajax request fails. `this` is bound to input element.
3838
* `onInvalidateSelection`: `function () {}` called when input is altered after selection has been made. `this` is bound to input element.
3939
* `triggerSelectOnValidInput`: Boolean value indicating if `select` should be triggered if it matches suggestion. Default `true`.
40-
* `preventBadQueries`: Boolean value indicating if it shoud prevent future ajax requests for queries with the same root if no results were returned. E.g. if `Jam` returns no suggestions, it will not fire for any future query that starts with `Jam`. Default `true`.
40+
* `preventBadQueries`: Boolean value indicating if it shoud prevent future ajax requests for queries with the same root if no results were returned. E.g. if `Jam` returns no suggestions, it will not fire for any future query that starts with `Jam`. Default `true`.
4141
* `beforeRender`: `function (container) {}` called before displaying the suggestions. You may manipulate suggestions DOM before it is displayed.
4242
* `tabDisabled`: Default `false`. Set to true to leave the cursor in the input field after the user tabs to select a suggestion.
4343
* `paramName`: Default `query`. The name of the request parameter that contains the query.
@@ -53,6 +53,7 @@ The standard jquery.autocomplete.js file is around 2.7KB when minified via Closu
5353
If set to `auto`, the suggestions will be orientated it the way that place them closer to middle of the view port.
5454
* `groupBy`: property name of the suggestion `data` object, by which results should be grouped.
5555
* `preserveInput`: if `true`, input value stays the same when navigating over suggestions. Default: `false`.
56+
* `onHide`: `function (container) {}` called before container will be hidden
5657

5758
Autocomplete instance has following methods:
5859

@@ -64,7 +65,7 @@ Autocomplete instance has following methods:
6465
* `hide`: hides suggestions.
6566
* `dispose`: destroys autocomplete instance. All events are detached and suggestion containers removed.
6667

67-
There are two ways that you can invoke Autocomplete method. One is calling autocomplete on jQuery object and passing method name as string literal.
68+
There are two ways that you can invoke Autocomplete method. One is calling autocomplete on jQuery object and passing method name as string literal.
6869
If method has arguments, arguments are passed as consecutive parameters:
6970

7071
```javascript
@@ -177,8 +178,8 @@ Response from the server must be JSON formatted following JavaScript object:
177178
}
178179
```
179180

180-
Data can be any value or object. Data object is passed to formatResults function
181-
and onSelect callback. Alternatively, if there is no data you can
181+
Data can be any value or object. Data object is passed to formatResults function
182+
and onSelect callback. Alternatively, if there is no data you can
182183
supply just a string array for suggestions:
183184

184185
```json
@@ -229,10 +230,10 @@ $('.autocomplete').devbridgeAutocomplete({ ... });
229230

230231
##License
231232

232-
Ajax Autocomplete for jQuery is freely distributable under the
233+
Ajax Autocomplete for jQuery is freely distributable under the
233234
terms of an MIT-style [license](https://github.com/devbridge/jQuery-Autocomplete/blob/master/dist/license.txt).
234235

235-
Copyright notice and permission notice shall be included in all
236+
Copyright notice and permission notice shall be included in all
236237
copies or substantial portions of the Software.
237238

238239
##Authors

spec/autocompleteBehavior.js

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/*jslint vars: true*/
1+
/*jslint vars: true*/
22
/*global describe, it, expect, waits, waitsFor, runs, afterEach, spyOn, $, beforeEach*/
33

44
describe('Autocomplete Async', function () {
@@ -703,6 +703,32 @@ describe('Autocomplete', function () {
703703
expect(suggestionsContainer.find('.autocomplete-no-suggestion').text()).toBe('Sorry, no matching results');
704704
});
705705

706+
it('Should call onHide and pass container jQuery object', function () {
707+
var element = document.createElement('input'),
708+
input = $(element),
709+
instance,
710+
elementCount,
711+
context;
712+
713+
input.autocomplete({
714+
lookup: [{ value: 'Jamaica', data: 'B' }],
715+
onHide: function (container) {
716+
context = this;
717+
elementCount = container.length;
718+
}
719+
});
720+
721+
input.val('Jam');
722+
instance = input.autocomplete();
723+
instance.onValueChange();
724+
725+
input.val('Colombia');
726+
instance.onValueChange();
727+
728+
expect(context).toBe(element);
729+
expect(elementCount).toBe(1);
730+
});
731+
706732
});
707733

708734
describe('When options.preserveInput is true', function () {

src/jquery.autocomplete.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -612,7 +612,14 @@
612612
},
613613

614614
hide: function () {
615-
var that = this;
615+
var that = this,
616+
container = $(that.suggestionsContainer);
617+
618+
if ($.isFunction(this.options.onHide) && that.visible) {
619+
620+
this.options.onHide.call(that.element, container);
621+
}
622+
616623
that.visible = false;
617624
that.selectedIndex = -1;
618625
clearInterval(that.onChangeInterval);
@@ -622,6 +629,7 @@
622629

623630
suggest: function () {
624631
if (this.suggestions.length === 0) {
632+
this.options.showNoSuggestionNotice ? this.noSuggestions() : this.hide();
625633
if (this.options.showNoSuggestionNotice) {
626634
this.noSuggestions();
627635
} else {

0 commit comments

Comments
 (0)