Skip to content
This repository was archived by the owner on May 29, 2019. It is now read-only.

Commit 90a8aa7

Browse files
trashpkozlowski-opensource
authored andcommitted
fix(typeahead): fixed waitTime functionality
Closes #573
1 parent a3890e7 commit 90a8aa7

File tree

2 files changed

+41
-5
lines changed

2 files changed

+41
-5
lines changed

src/typeahead/test/typeahead.spec.js

+35
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,41 @@ describe('typeahead tests', function () {
183183
$timeout.flush();
184184
expect(element).toBeOpenWithActive(1, 0);
185185
}));
186+
187+
it('should cancel old timeouts when something is typed within waitTime', inject(function ($timeout) {
188+
var values = [];
189+
$scope.loadMatches = function(viewValue) {
190+
values.push(viewValue);
191+
return $scope.source;
192+
};
193+
var element = prepareInputEl("<div><input ng-model='result' typeahead='item for item in loadMatches($viewValue) | filter:$viewValue' typeahead-wait-ms='200'></div>");
194+
changeInputValueTo(element, 'first');
195+
changeInputValueTo(element, 'second');
196+
197+
$timeout.flush();
198+
199+
expect(values).not.toContain('first');
200+
}));
201+
202+
it('should allow timeouts when something is typed after waitTime has passed', inject(function ($timeout) {
203+
var values = [];
204+
205+
$scope.loadMatches = function(viewValue) {
206+
values.push(viewValue);
207+
return $scope.source;
208+
};
209+
var element = prepareInputEl("<div><input ng-model='result' typeahead='item for item in loadMatches($viewValue) | filter:$viewValue' typeahead-wait-ms='200'></div>");
210+
211+
changeInputValueTo(element, 'first');
212+
$timeout.flush();
213+
214+
expect(values).toContain('first');
215+
216+
changeInputValueTo(element, 'second');
217+
$timeout.flush();
218+
219+
expect(values).toContain('second');
220+
}));
186221
});
187222

188223
describe('selecting a match', function () {

src/typeahead/typeahead.js

+6-5
Original file line numberDiff line numberDiff line change
@@ -133,19 +133,20 @@ angular.module('ui.bootstrap.typeahead', ['ui.bootstrap.position'])
133133
//we need to propagate user's query so we can higlight matches
134134
scope.query = undefined;
135135

136+
//Declare the timeout promise var outside the function scope so that stacked calls can be cancelled later
137+
var timeoutPromise;
138+
136139
//plug into $parsers pipeline to open a typeahead on view changes initiated from DOM
137140
//$parsers kick-in on all the changes coming from the view as well as manually triggered by $setViewValue
138141
modelCtrl.$parsers.push(function (inputValue) {
139142

140-
var timeoutId;
141-
142143
resetMatches();
143144
if (inputValue && inputValue.length >= minSearch) {
144145
if (waitTime > 0) {
145-
if (timeoutId) {
146-
$timeout.cancel(timeoutId);//cancel previous timeout
146+
if (timeoutPromise) {
147+
$timeout.cancel(timeoutPromise);//cancel previous timeout
147148
}
148-
timeoutId = $timeout(function () {
149+
timeoutPromise = $timeout(function () {
149150
getMatchesAsync(inputValue);
150151
}, waitTime);
151152
} else {

0 commit comments

Comments
 (0)