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

Commit

Permalink
fix(autocomplete): promises that resolve immediately will work properly
Browse files Browse the repository at this point in the history
Closes #3117
  • Loading branch information
Robert Messerle committed Jul 13, 2015
1 parent c5b3131 commit b316bba
Showing 1 changed file with 14 additions and 22 deletions.
36 changes: 14 additions & 22 deletions src/components/autocomplete/js/autocompleteController.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [],
Expand Down Expand Up @@ -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();
Expand All @@ -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(),
Expand All @@ -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.
Expand Down Expand Up @@ -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(); });
}
}

Expand Down Expand Up @@ -319,7 +316,7 @@ function MdAutocompleteCtrl ($scope, $element, $mdUtil, $mdConstant, $timeout, $
}
}
});

}

/**
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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];
Expand Down

0 comments on commit b316bba

Please sign in to comment.