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

Commit

Permalink
fix(autocomplete): stop loading if last promise got resolved
Browse files Browse the repository at this point in the history
> 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
  • Loading branch information
devversion authored and ThomasBurleson committed Mar 30, 2016
1 parent a2ac9a3 commit e372cf9
Showing 1 changed file with 10 additions and 5 deletions.
15 changes: 10 additions & 5 deletions src/components/autocomplete/js/autocompleteController.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ function MdAutocompleteCtrl ($scope, $element, $mdUtil, $mdConstant, $mdTheming,
selectedItemWatchers = [],
hasFocus = false,
lastCount = 0,
promiseFetch = false;
fetchesInProgress = 0;

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

items = $q.when(items);
fetchesInProgress++;
setLoading(true);
promiseFetch = true;

$mdUtil.nextTick(function () {
items
.then(handleResults)
.finally(function(){
setLoading(false);
promiseFetch = false;
if (--fetchesInProgress === 0) {
setLoading(false);
}
});
},true, $scope);
}
Expand Down Expand Up @@ -715,14 +716,18 @@ function MdAutocompleteCtrl ($scope, $element, $mdUtil, $mdConstant, $mdTheming,
}
}

function isPromiseFetching() {
return fetchesInProgress !== 0;
}

function scrollTo (offset) {
elements.$.scrollContainer.controller('mdVirtualRepeatContainer').scrollTo(offset);
}

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

return ctrl.hasNotFound && !hasMatches() && (!ctrl.loading || promiseFetch) && textLength >= getMinLength() && (hasFocus || noBlur) && !hasSelection();
return ctrl.hasNotFound && !hasMatches() && (!ctrl.loading || isPromiseFetching()) && textLength >= getMinLength() && (hasFocus || noBlur) && !hasSelection();
}

/**
Expand Down

0 comments on commit e372cf9

Please sign in to comment.