From b316bba9b5a8158079a266eedad70b1fb59f880b Mon Sep 17 00:00:00 2001 From: Robert Messerle Date: Mon, 13 Jul 2015 12:14:43 -0700 Subject: [PATCH] fix(autocomplete): promises that resolve immediately will work properly Closes #3117 --- .../autocomplete/js/autocompleteController.js | 36 ++++++++----------- 1 file changed, 14 insertions(+), 22 deletions(-) diff --git a/src/components/autocomplete/js/autocompleteController.js b/src/components/autocomplete/js/autocompleteController.js index 9b7e1f1a930..4f543bffeef 100644 --- a/src/components/autocomplete/js/autocompleteController.js +++ b/src/components/autocomplete/js/autocompleteController.js @@ -6,14 +6,13 @@ var ITEM_HEIGHT = 41, MAX_HEIGHT = 5.5 * ITEM_HEIGHT, MENU_PADDING = 8; -function MdAutocompleteCtrl ($scope, $element, $mdUtil, $mdConstant, $timeout, $mdTheming, $window, +function MdAutocompleteCtrl ($scope, $element, $mdUtil, $mdConstant, $mdTheming, $window, $animate, $rootElement, $attrs, $q) { //-- private variables var ctrl = this, itemParts = $scope.itemsExpr.split(/ in /i), itemExpr = itemParts[1], elements = null, - promise = null, cache = {}, noBlur = false, selectedItemWatchers = [], @@ -60,7 +59,7 @@ function MdAutocompleteCtrl ($scope, $element, $mdUtil, $mdConstant, $timeout, $ $mdUtil.initOptionalProperties($scope, $attrs, { searchText: null, selectedItem: null } ); $mdTheming($element); configureWatchers(); - $timeout(function () { + $mdUtil.nextTick(function () { gatherElements(); focusElement(); moveDropdown(); @@ -72,7 +71,7 @@ function MdAutocompleteCtrl ($scope, $element, $mdUtil, $mdConstant, $timeout, $ * @returns {*} */ function positionDropdown () { - if (!elements) return $timeout(positionDropdown, 0, false); + if (!elements) return $mdUtil.nextTick(positionDropdown); var hrect = elements.wrap.getBoundingClientRect(), vrect = elements.snap.getBoundingClientRect(), root = elements.root.getBoundingClientRect(), @@ -95,7 +94,7 @@ function MdAutocompleteCtrl ($scope, $element, $mdUtil, $mdConstant, $timeout, $ styles.maxHeight = Math.min(MAX_HEIGHT, root.bottom - hrect.bottom - MENU_PADDING) + 'px'; } elements.$.ul.css(styles); - $timeout(correctHorizontalAlignment, 0, false); + $mdUtil.nextTick(correctHorizontalAlignment); /** * Makes sure that the menu doesn't go off of the screen on either side. @@ -200,11 +199,9 @@ function MdAutocompleteCtrl ($scope, $element, $mdUtil, $mdConstant, $timeout, $ function handleHiddenChange (hidden, oldHidden) { if (!hidden && oldHidden) { positionDropdown(); - if (elements) $timeout(function () { $mdUtil.disableScrollAround(elements.ul); }, 0, false); + if (elements) $mdUtil.nextTick(function () { $mdUtil.disableScrollAround(elements.ul); }); } else if (hidden && !oldHidden) { - $timeout(function() { - $mdUtil.enableScrolling(); - }, 0, false); + $mdUtil.nextTick(function() { $mdUtil.enableScrolling(); }); } } @@ -319,7 +316,7 @@ function MdAutocompleteCtrl ($scope, $element, $mdUtil, $mdConstant, $timeout, $ } } }); - + } /** @@ -478,7 +475,7 @@ function MdAutocompleteCtrl ($scope, $element, $mdUtil, $mdConstant, $timeout, $ */ function select(index) { //-- force form to update state for validation - $timeout(function() { + $mdUtil.nextTick(function() { getDisplayValue(ctrl.matches[index]).then(function(val) { var ngModel = elements.$.input.controller('ngModel'); ngModel.$setViewValue(val); @@ -519,16 +516,16 @@ function MdAutocompleteCtrl ($scope, $element, $mdUtil, $mdConstant, $timeout, $ if (angular.isArray(items)) { handleResults(items); } else if (items) { - ctrl.loading = true; - if (items.success) items.success(handleResults); - if (items.then) items.then(handleResults); - if (items.error) items.error(function () { ctrl.loading = false; }); + $mdUtil.nextTick(function () { + ctrl.loading = true; + if (items.success) items.success(handleResults); + if (items.then) items.then(handleResults); + if (items.finally) items.finally(function () { ctrl.loading = false; }); + }); } function handleResults (matches) { cache[term] = matches; if (searchText !== $scope.searchText) return; //-- just cache the results if old request - ctrl.loading = false; - promise = null; ctrl.matches = matches; ctrl.hidden = shouldHide(); updateMessages(); @@ -582,11 +579,6 @@ function MdAutocompleteCtrl ($scope, $element, $mdUtil, $mdConstant, $timeout, $ function handleQuery () { var searchText = $scope.searchText, term = searchText.toLowerCase(); - //-- cancel promise if a promise is in progress - if (promise && promise.cancel) { - promise.cancel(); - promise = null; - } //-- if results are cached, pull in cached results if (!$scope.noCache && cache[term]) { ctrl.matches = cache[term];