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

fix(autocomplete): improve promise logic #6521

Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions src/components/autocomplete/js/autocompleteController.js
Original file line number Diff line number Diff line change
Expand Up @@ -641,9 +641,15 @@ function MdAutocompleteCtrl ($scope, $element, $mdUtil, $mdConstant, $mdTheming,
$mdUtil.nextTick(function () {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not use the $q.when( ) to wrap angular & foreign promises:

function fetchResults (searchText) {
  var items = $scope.$parent.$eval(itemExpr),
      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);

    $mdUtil.nextTick(function () {      
        items.finally(function(){ 
          setLoading(false); 
        });
    },true, $scope);
  }
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PR #6860 also suggest not listening for .success( ).

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ThomasBurleson Probably a good point, but this is a complaint on the original code, not this PR.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@winniehell - please incorporate my suggestions ^^ and test. This will make the merged code better moving forward.

if (items.success) items.success(handleResults);
if (items.then) items.then(handleResults);
if (items.finally) items.finally(function () {
setLoading(false);
});
if (items.finally) {
items.finally(function () {
setLoading(false);
});
} else if (items.then) {
items.then(function () {
setLoading(false);
});
}
},true, $scope);
}
function handleResults (matches) {
Expand Down