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

Commit

Permalink
fix(autocomplete): improve promise logic
Browse files Browse the repository at this point in the history
Thx @winniehell.

Closes #6521.
  • Loading branch information
ThomasBurleson committed Feb 4, 2016
1 parent 176a2f9 commit 4d59a61
Showing 1 changed file with 18 additions and 10 deletions.
28 changes: 18 additions & 10 deletions src/components/autocomplete/js/autocompleteController.js
Original file line number Diff line number Diff line change
Expand Up @@ -634,21 +634,29 @@ function MdAutocompleteCtrl ($scope, $element, $mdUtil, $mdConstant, $mdTheming,
*/
function fetchResults (searchText) {
var items = $scope.$parent.$eval(itemExpr),
term = searchText.toLowerCase();
if (angular.isArray(items)) {
handleResults(items);
} else if (items) {
term = searchText.toLowerCase(),
isList = angular.isArray(items);

if ( isList ) handleResults(items);
else handleAsyncResults(items);

function handleAsyncResults(items) {
if ( !items ) return;

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

$mdUtil.nextTick(function () {
if (items.success) items.success(handleResults);
if (items.then) items.then(handleResults);
if (items.finally) items.finally(function () {
setLoading(false);
promiseFetch = false;
});
items
.then(handleResults)
.finally(function(){
setLoading(false);
promiseFetch = false;
});
},true, $scope);
}

function handleResults (matches) {
cache[ term ] = matches;
if ((searchText || '') !== ($scope.searchText || '')) return; //-- just cache the results if old request
Expand Down

0 comments on commit 4d59a61

Please sign in to comment.