From 6feafc1031ae1de8b0e203810a486323ebe36c30 Mon Sep 17 00:00:00 2001 From: Paul Gschwendtner Date: Tue, 2 Feb 2016 18:06:33 +0100 Subject: [PATCH] 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 --- .../autocomplete/js/autocompleteController.js | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/src/components/autocomplete/js/autocompleteController.js b/src/components/autocomplete/js/autocompleteController.js index 665dab4381f..194f9845186 100644 --- a/src/components/autocomplete/js/autocompleteController.js +++ b/src/components/autocomplete/js/autocompleteController.js @@ -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); @@ -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); } @@ -715,6 +716,10 @@ function MdAutocompleteCtrl ($scope, $element, $mdUtil, $mdConstant, $mdTheming, } } + function isPromiseFetching() { + return fetchesInProgress !== 0; + } + function scrollTo (offset) { elements.$.scrollContainer.controller('mdVirtualRepeatContainer').scrollTo(offset); } @@ -722,7 +727,7 @@ function MdAutocompleteCtrl ($scope, $element, $mdUtil, $mdConstant, $mdTheming, 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(); } /**