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

Commit b316bba

Browse files
author
Robert Messerle
committed
fix(autocomplete): promises that resolve immediately will work properly
Closes #3117
1 parent c5b3131 commit b316bba

File tree

1 file changed

+14
-22
lines changed

1 file changed

+14
-22
lines changed

src/components/autocomplete/js/autocompleteController.js

Lines changed: 14 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,13 @@ var ITEM_HEIGHT = 41,
66
MAX_HEIGHT = 5.5 * ITEM_HEIGHT,
77
MENU_PADDING = 8;
88

9-
function MdAutocompleteCtrl ($scope, $element, $mdUtil, $mdConstant, $timeout, $mdTheming, $window,
9+
function MdAutocompleteCtrl ($scope, $element, $mdUtil, $mdConstant, $mdTheming, $window,
1010
$animate, $rootElement, $attrs, $q) {
1111
//-- private variables
1212
var ctrl = this,
1313
itemParts = $scope.itemsExpr.split(/ in /i),
1414
itemExpr = itemParts[1],
1515
elements = null,
16-
promise = null,
1716
cache = {},
1817
noBlur = false,
1918
selectedItemWatchers = [],
@@ -60,7 +59,7 @@ function MdAutocompleteCtrl ($scope, $element, $mdUtil, $mdConstant, $timeout, $
6059
$mdUtil.initOptionalProperties($scope, $attrs, { searchText: null, selectedItem: null } );
6160
$mdTheming($element);
6261
configureWatchers();
63-
$timeout(function () {
62+
$mdUtil.nextTick(function () {
6463
gatherElements();
6564
focusElement();
6665
moveDropdown();
@@ -72,7 +71,7 @@ function MdAutocompleteCtrl ($scope, $element, $mdUtil, $mdConstant, $timeout, $
7271
* @returns {*}
7372
*/
7473
function positionDropdown () {
75-
if (!elements) return $timeout(positionDropdown, 0, false);
74+
if (!elements) return $mdUtil.nextTick(positionDropdown);
7675
var hrect = elements.wrap.getBoundingClientRect(),
7776
vrect = elements.snap.getBoundingClientRect(),
7877
root = elements.root.getBoundingClientRect(),
@@ -95,7 +94,7 @@ function MdAutocompleteCtrl ($scope, $element, $mdUtil, $mdConstant, $timeout, $
9594
styles.maxHeight = Math.min(MAX_HEIGHT, root.bottom - hrect.bottom - MENU_PADDING) + 'px';
9695
}
9796
elements.$.ul.css(styles);
98-
$timeout(correctHorizontalAlignment, 0, false);
97+
$mdUtil.nextTick(correctHorizontalAlignment);
9998

10099
/**
101100
* 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, $
200199
function handleHiddenChange (hidden, oldHidden) {
201200
if (!hidden && oldHidden) {
202201
positionDropdown();
203-
if (elements) $timeout(function () { $mdUtil.disableScrollAround(elements.ul); }, 0, false);
202+
if (elements) $mdUtil.nextTick(function () { $mdUtil.disableScrollAround(elements.ul); });
204203
} else if (hidden && !oldHidden) {
205-
$timeout(function() {
206-
$mdUtil.enableScrolling();
207-
}, 0, false);
204+
$mdUtil.nextTick(function() { $mdUtil.enableScrolling(); });
208205
}
209206
}
210207

@@ -319,7 +316,7 @@ function MdAutocompleteCtrl ($scope, $element, $mdUtil, $mdConstant, $timeout, $
319316
}
320317
}
321318
});
322-
319+
323320
}
324321

325322
/**
@@ -478,7 +475,7 @@ function MdAutocompleteCtrl ($scope, $element, $mdUtil, $mdConstant, $timeout, $
478475
*/
479476
function select(index) {
480477
//-- force form to update state for validation
481-
$timeout(function() {
478+
$mdUtil.nextTick(function() {
482479
getDisplayValue(ctrl.matches[index]).then(function(val) {
483480
var ngModel = elements.$.input.controller('ngModel');
484481
ngModel.$setViewValue(val);
@@ -519,16 +516,16 @@ function MdAutocompleteCtrl ($scope, $element, $mdUtil, $mdConstant, $timeout, $
519516
if (angular.isArray(items)) {
520517
handleResults(items);
521518
} else if (items) {
522-
ctrl.loading = true;
523-
if (items.success) items.success(handleResults);
524-
if (items.then) items.then(handleResults);
525-
if (items.error) items.error(function () { ctrl.loading = false; });
519+
$mdUtil.nextTick(function () {
520+
ctrl.loading = true;
521+
if (items.success) items.success(handleResults);
522+
if (items.then) items.then(handleResults);
523+
if (items.finally) items.finally(function () { ctrl.loading = false; });
524+
});
526525
}
527526
function handleResults (matches) {
528527
cache[term] = matches;
529528
if (searchText !== $scope.searchText) return; //-- just cache the results if old request
530-
ctrl.loading = false;
531-
promise = null;
532529
ctrl.matches = matches;
533530
ctrl.hidden = shouldHide();
534531
updateMessages();
@@ -582,11 +579,6 @@ function MdAutocompleteCtrl ($scope, $element, $mdUtil, $mdConstant, $timeout, $
582579
function handleQuery () {
583580
var searchText = $scope.searchText,
584581
term = searchText.toLowerCase();
585-
//-- cancel promise if a promise is in progress
586-
if (promise && promise.cancel) {
587-
promise.cancel();
588-
promise = null;
589-
}
590582
//-- if results are cached, pull in cached results
591583
if (!$scope.noCache && cache[term]) {
592584
ctrl.matches = cache[term];

0 commit comments

Comments
 (0)