Skip to content

Commit 361cc1e

Browse files
committed
Fix #438 Too many ajax calls with multiple groups
1 parent 04ea0b9 commit 361cc1e

File tree

5 files changed

+120
-8
lines changed

5 files changed

+120
-8
lines changed

dist/jquery.typeahead.min.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/jquery.typeahead.js

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* Licensed under the MIT license
55
*
66
* @author Tom Bertrand
7-
* @version 2.10.5 (2018-7-20)
7+
* @version 2.10.5 (2018-7-24)
88
* @link http://www.runningcoder.org/jquerytypeahead/
99
*/
1010
(function (factory) {
@@ -813,6 +813,14 @@
813813
this.query.length >= this.options.source[group].minLength &&
814814
this.query.length <= this.options.source[group].maxLength
815815
) {
816+
if (
817+
this.filters.dropdown &&
818+
this.filters.dropdown.key === 'group' &&
819+
this.filters.dropdown.value !== group
820+
) {
821+
continue;
822+
}
823+
816824
this.searchGroups.push(group);
817825
if (!this.options.source[group].dynamic && this.source[group]) {
818826
continue;
@@ -1119,7 +1127,7 @@
11191127
_group = xhrObject.validForGroup[i];
11201128
_request = scope.requests[_group];
11211129

1122-
if (_request.callback.done instanceof Function) {
1130+
if (typeof _request.callback.done === 'function') {
11231131
_groupData[_group] = _request.callback.done.call(
11241132
scope,
11251133
data,
@@ -2858,7 +2866,7 @@
28582866
.html(item.template);
28592867

28602868
this.isDropdownEvent = true;
2861-
this.node.trigger("search" + this.namespace);
2869+
this.node.trigger("input" + this.namespace);
28622870

28632871
if (this.options.multiselect) {
28642872
this.adjustInputSize();

test/integration/ajax.callback.done.test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ describe('Typeahead $.ajax.callback.done Tests', () => {
2121
source: {
2222
group1: {
2323
ajax: {
24-
type: "POST",
24+
type: "GET",
2525
url: "http://test.com/groups.json",
2626
data: {
2727
term: "{{query}}"
@@ -35,7 +35,7 @@ describe('Typeahead $.ajax.callback.done Tests', () => {
3535
},
3636
group2: {
3737
ajax: {
38-
type: "POST",
38+
type: "GET",
3939
url: "http://test.com/groups.json",
4040
data: {
4141
term: "{{query}}"
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
const $ = require("jquery");
2+
const Typeahead = require('../../src/jquery.typeahead');
3+
4+
describe('Typeahead $.ajax.callback.done Tests', () => {
5+
6+
let myTypeahead;
7+
const group1DoneCallback = jest.fn();
8+
const group2DoneCallback = jest.fn();
9+
const group3DoneCallback = jest.fn();
10+
11+
beforeAll(() => {
12+
13+
document.body.innerHTML = `<form>
14+
<div class="typeahead__container">
15+
<div class="typeahead__field">
16+
<div class="typeahead__query">
17+
<input class="js-typeahead"
18+
name="q"
19+
type="search"
20+
autofocus
21+
autocomplete="off">
22+
</div>
23+
<div class="typeahead__button">
24+
<button type="submit">
25+
<span class="typeahead__search-icon"></span>
26+
</button>
27+
</div>
28+
</div>
29+
</div>
30+
</form>`;
31+
32+
myTypeahead = $.typeahead({
33+
input: ".js-typeahead",
34+
minLength: 0,
35+
group: true,
36+
maxItemPerGroup: 3,
37+
dynamic: true,
38+
dropdownFilter: "all",
39+
source: {
40+
group1: {
41+
ajax: {
42+
url: "http://test.com/game.json",
43+
path: "data",
44+
callback: {
45+
done: group1DoneCallback
46+
}
47+
}
48+
},
49+
group2: {
50+
ajax: {
51+
url: "http://test.com/category.json",
52+
path: "data",
53+
callback: {
54+
done: group2DoneCallback
55+
}
56+
}
57+
},
58+
group3: {
59+
ajax: {
60+
url: "http://test.com/tag.json",
61+
path: "data",
62+
callback: {
63+
done: group3DoneCallback
64+
}
65+
}
66+
},
67+
},
68+
});
69+
});
70+
71+
afterEach(() => {
72+
jest.resetAllMocks();
73+
});
74+
75+
it('Should trigger all group callbacks', (done) => {
76+
77+
myTypeahead.node.triggerHandler('input').done(function () {
78+
expect(myTypeahead.searchGroups).toEqual(['group1', 'group2', 'group3'])
79+
expect(group1DoneCallback).toHaveBeenCalled();
80+
expect(group2DoneCallback).toHaveBeenCalled();
81+
expect(group3DoneCallback).toHaveBeenCalled();
82+
83+
done()
84+
});
85+
86+
});
87+
88+
it('Should only trigger the selected group callback', (done) => {
89+
90+
let dropdownItems = myTypeahead.container.find('.' + myTypeahead.options.selector.dropdownItem);
91+
dropdownItems.eq(0).find('a').trigger('click');
92+
93+
myTypeahead.node.triggerHandler('input').done(function () {
94+
expect(myTypeahead.searchGroups).toEqual(['group1']);
95+
expect(group1DoneCallback).toHaveBeenCalled();
96+
expect(group2DoneCallback).not.toHaveBeenCalled();
97+
expect(group3DoneCallback).not.toHaveBeenCalled();
98+
99+
done()
100+
});
101+
102+
});
103+
104+
});

test/jestSetupFile.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ fakeServer.respondWith('GET', /\/empty\.json/, [
4343
JSON.stringify([]),
4444
]);
4545

46-
fakeServer.respondWith('POST', /\/groups\.json/, [
46+
fakeServer.respondWith('GET', /\/groups\.json/, [
4747
200,
4848
{ 'Content-Type': 'application/json' },
4949
JSON.stringify({

0 commit comments

Comments
 (0)