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

Commit 5dc5792

Browse files
Michal Wesolowskipkozlowski-opensource
Michal Wesolowski
authored andcommitted
fix(typeahead): timeout cancellation when deleting characters
Closes #2074
1 parent 941a382 commit 5dc5792

File tree

2 files changed

+30
-6
lines changed

2 files changed

+30
-6
lines changed

src/typeahead/test/typeahead.spec.js

+15
Original file line numberDiff line numberDiff line change
@@ -555,6 +555,21 @@ describe('typeahead tests', function () {
555555
expect($scope.isLoading).toBeFalsy();
556556
});
557557

558+
it('should cancel old timeout when deleting characters', inject(function ($timeout) {
559+
var values = [];
560+
$scope.loadMatches = function(viewValue) {
561+
values.push(viewValue);
562+
return $scope.source;
563+
};
564+
var element = prepareInputEl('<div><input ng-model="result" typeahead="item for item in loadMatches($viewValue) | filter:$viewValue" typeahead-min-length="2" typeahead-wait-ms="200"></div>');
565+
changeInputValueTo(element, 'match');
566+
changeInputValueTo(element, 'm');
567+
568+
$timeout.flush();
569+
570+
expect(values).not.toContain('match');
571+
}));
572+
558573
it('does not close matches popup on click in input', function () {
559574
var element = prepareInputEl('<div><input ng-model="result" typeahead="item for item in source | filter:$viewValue"></div>');
560575
var inputEl = findInput(element);

src/typeahead/typeahead.js

+15-6
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,18 @@ angular.module('ui.bootstrap.typeahead', ['ui.bootstrap.position', 'ui.bootstrap
173173
//Declare the timeout promise var outside the function scope so that stacked calls can be cancelled later
174174
var timeoutPromise;
175175

176+
var scheduleSearchWithTimeout = function(inputValue) {
177+
timeoutPromise = $timeout(function () {
178+
getMatchesAsync(inputValue);
179+
}, waitTime);
180+
};
181+
182+
var cancelPreviousTimeout = function() {
183+
if (timeoutPromise) {
184+
$timeout.cancel(timeoutPromise);
185+
}
186+
};
187+
176188
//plug into $parsers pipeline to open a typeahead on view changes initiated from DOM
177189
//$parsers kick-in on all the changes coming from the view as well as manually triggered by $setViewValue
178190
modelCtrl.$parsers.unshift(function (inputValue) {
@@ -181,17 +193,14 @@ angular.module('ui.bootstrap.typeahead', ['ui.bootstrap.position', 'ui.bootstrap
181193

182194
if (inputValue && inputValue.length >= minSearch) {
183195
if (waitTime > 0) {
184-
if (timeoutPromise) {
185-
$timeout.cancel(timeoutPromise);//cancel previous timeout
186-
}
187-
timeoutPromise = $timeout(function () {
188-
getMatchesAsync(inputValue);
189-
}, waitTime);
196+
cancelPreviousTimeout();
197+
scheduleSearchWithTimeout(inputValue);
190198
} else {
191199
getMatchesAsync(inputValue);
192200
}
193201
} else {
194202
isLoadingSetter(originalScope, false);
203+
cancelPreviousTimeout();
195204
resetMatches();
196205
}
197206

0 commit comments

Comments
 (0)