-
Notifications
You must be signed in to change notification settings - Fork 3.4k
fix(autocomplete): improve promise logic #6521
fix(autocomplete): improve promise logic #6521
Conversation
Thanks for your pull request. It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA). 📝 Please visit https://cla.developers.google.com/ to sign. Once you've signed, please reply here (e.g.
|
@googlebot I signed it! |
CLAs look good, thanks! |
48f668d
to
a3fd0e9
Compare
Failing tests seem to be unrelated as they appear e.g. in #6515 (build 99782625) and #6518 (build 99851677) as well. Not sure what I can do about it... 👽❓ |
The tests of the whole project are currently failing. That's not an issue from you ;) And now to the PR, I thought we using here the Promise Service from Angular. See here. This a implementation inspired by https://github.com/kriskowal/q Am I wrong? |
@devversion you are right that it is usual to use There are two reasons for me not to use
|
Can be true, I don't know about A+ etc. But in this context of code we using DI to get the $q service of Angular, so there won't be any other Deferred API, that's why I think this change is not needed. |
Even though |
For clarification: the effect on |
always reset loading state of autocomplete after handleResults() even if promise implementation does not support finally()
a3fd0e9
to
3588c0a
Compare
Tests are now all green. |
@@ -641,9 +641,15 @@ function MdAutocompleteCtrl ($scope, $element, $mdUtil, $mdConstant, $mdTheming, | |||
$mdUtil.nextTick(function () { |
There was a problem hiding this comment.
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);
}
}
There was a problem hiding this comment.
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( )
.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
@robertmesserle - can you review? |
@ThomasBurleson LGTM |
@ThomasBurleson: I'll try to rework this including your comments and a fix for #6860. |
@ThomasBurleson: Alright, I was still trying to figure out, how to write a test for it. Thank you for fixing this! 👍 😃 |
The
finally
-function supported by q is not part of the A+ spec. Therefore, other promise implementations do not have it (e.g. the promise polyfill in babel). To ensure thatmd-autocomplete
resets its loading state properly, I usedthen
iffinally
is not available.This pull request was joined with #6860.