@@ -732,7 +732,7 @@ function MdAutocompleteCtrl ($scope, $element, $mdUtil, $mdConstant, $mdTheming,
732
732
isList = angular . isArray ( items ) ,
733
733
isPromise = ! ! items . then ; // Every promise should contain a `then` property
734
734
735
- if ( isList ) handleResults ( items ) ;
735
+ if ( isList ) onResultsRetrieved ( items ) ;
736
736
else if ( isPromise ) handleAsyncResults ( items ) ;
737
737
738
738
function handleAsyncResults ( items ) {
@@ -744,7 +744,7 @@ function MdAutocompleteCtrl ($scope, $element, $mdUtil, $mdConstant, $mdTheming,
744
744
745
745
$mdUtil . nextTick ( function ( ) {
746
746
items
747
- . then ( handleResults )
747
+ . then ( onResultsRetrieved )
748
748
. finally ( function ( ) {
749
749
if ( -- fetchesInProgress === 0 ) {
750
750
setLoading ( false ) ;
@@ -753,21 +753,16 @@ function MdAutocompleteCtrl ($scope, $element, $mdUtil, $mdConstant, $mdTheming,
753
753
} , true , $scope ) ;
754
754
}
755
755
756
- function handleResults ( matches ) {
757
- cache [ term ] = matches ;
758
- if ( ( searchText || '' ) !== ( $scope . searchText || '' ) ) return ; //-- just cache the results if old request
756
+ function onResultsRetrieved ( matches ) {
757
+ cache [ term ] = matches ;
759
758
760
- ctrl . matches = matches ;
761
- ctrl . hidden = shouldHide ( ) ;
762
-
763
- // If loading is in progress, then we'll end the progress. This is needed for example,
764
- // when the `clear` button was clicked, because there we always show the loading process, to prevent flashing.
765
- if ( ctrl . loading ) setLoading ( false ) ;
766
-
767
- if ( $scope . selectOnMatch ) selectItemOnMatch ( ) ;
759
+ // Just cache the results if the request is now outdated.
760
+ // The request becomes outdated, when the new searchText has changed during the result fetching.
761
+ if ( ( searchText || '' ) !== ( $scope . searchText || '' ) ) {
762
+ return ;
763
+ }
768
764
769
- updateMessages ( ) ;
770
- positionDropdown ( ) ;
765
+ handleResults ( matches ) ;
771
766
}
772
767
}
773
768
@@ -833,20 +828,38 @@ function MdAutocompleteCtrl ($scope, $element, $mdUtil, $mdConstant, $mdTheming,
833
828
* results first, then forwards the process to `fetchResults` if necessary.
834
829
*/
835
830
function handleQuery ( ) {
836
- var searchText = $scope . searchText || '' ,
837
- term = searchText . toLowerCase ( ) ;
838
- //-- if results are cached, pull in cached results
839
- if ( ! $scope . noCache && cache [ term ] ) {
840
- ctrl . matches = cache [ term ] ;
841
- updateMessages ( ) ;
842
- setLoading ( false ) ;
831
+ var searchText = $scope . searchText || '' ;
832
+ var term = searchText . toLowerCase ( ) ;
833
+
834
+ // If caching is enabled and the current searchText is stored in the cache
835
+ if ( ! $scope . noCache && cache [ term ] ) {
836
+ // The results should be handled as same as a normal un-cached request does.
837
+ handleResults ( cache [ term ] ) ;
843
838
} else {
844
839
fetchResults ( searchText ) ;
845
840
}
846
841
847
842
ctrl . hidden = shouldHide ( ) ;
848
843
}
849
844
845
+ /**
846
+ * Handles the retrieved results by showing them in the autocompletes dropdown.
847
+ * @param results Retrieved results
848
+ */
849
+ function handleResults ( results ) {
850
+ ctrl . matches = results ;
851
+ ctrl . hidden = shouldHide ( ) ;
852
+
853
+ // If loading is in progress, then we'll end the progress. This is needed for example,
854
+ // when the `clear` button was clicked, because there we always show the loading process, to prevent flashing.
855
+ if ( ctrl . loading ) setLoading ( false ) ;
856
+
857
+ if ( $scope . selectOnMatch ) selectItemOnMatch ( ) ;
858
+
859
+ updateMessages ( ) ;
860
+ positionDropdown ( ) ;
861
+ }
862
+
850
863
/**
851
864
* If there is only one matching item and the search text matches its display value exactly,
852
865
* automatically select that item. Note: This function is only called if the user uses the
0 commit comments