Skip to content
This repository was archived by the owner on Sep 5, 2024. It is now read-only.

Commit e372cf9

Browse files
devversionThomasBurleson
authored andcommitted
fix(autocomplete): stop loading if last promise got resolved
> Just investigated a bit, the problem is, the autocomplete will set the loading state to false if a previous promise is resolved. So when you search for A-R-I-Z-O-N-A (normally typed - not pasted) there will be a promise for each char. So the state of setLoading will change to false. This can be solved by adding a simple fetching queue. But only check the queue on the promise finally to store the retrieved results in cache. Fixes #6907 Closes #6927
1 parent a2ac9a3 commit e372cf9

File tree

1 file changed

+10
-5
lines changed

1 file changed

+10
-5
lines changed

src/components/autocomplete/js/autocompleteController.js

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ function MdAutocompleteCtrl ($scope, $element, $mdUtil, $mdConstant, $mdTheming,
1919
selectedItemWatchers = [],
2020
hasFocus = false,
2121
lastCount = 0,
22-
promiseFetch = false;
22+
fetchesInProgress = 0;
2323

2424
//-- public variables with handlers
2525
defineProperty('hidden', handleHiddenChange, true);
@@ -648,15 +648,16 @@ function MdAutocompleteCtrl ($scope, $element, $mdUtil, $mdConstant, $mdTheming,
648648
if ( !items ) return;
649649

650650
items = $q.when(items);
651+
fetchesInProgress++;
651652
setLoading(true);
652-
promiseFetch = true;
653653

654654
$mdUtil.nextTick(function () {
655655
items
656656
.then(handleResults)
657657
.finally(function(){
658-
setLoading(false);
659-
promiseFetch = false;
658+
if (--fetchesInProgress === 0) {
659+
setLoading(false);
660+
}
660661
});
661662
},true, $scope);
662663
}
@@ -715,14 +716,18 @@ function MdAutocompleteCtrl ($scope, $element, $mdUtil, $mdConstant, $mdTheming,
715716
}
716717
}
717718

719+
function isPromiseFetching() {
720+
return fetchesInProgress !== 0;
721+
}
722+
718723
function scrollTo (offset) {
719724
elements.$.scrollContainer.controller('mdVirtualRepeatContainer').scrollTo(offset);
720725
}
721726

722727
function notFoundVisible () {
723728
var textLength = (ctrl.scope.searchText || '').length;
724729

725-
return ctrl.hasNotFound && !hasMatches() && (!ctrl.loading || promiseFetch) && textLength >= getMinLength() && (hasFocus || noBlur) && !hasSelection();
730+
return ctrl.hasNotFound && !hasMatches() && (!ctrl.loading || isPromiseFetching()) && textLength >= getMinLength() && (hasFocus || noBlur) && !hasSelection();
726731
}
727732

728733
/**

0 commit comments

Comments
 (0)