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

Commit 7430e68

Browse files
epelcThomasBurleson
authored andcommitted
feat(autocomplete): Add promise support to md-item-text
This reverts commit 727b1c1.
1 parent 5fdcf90 commit 7430e68

File tree

2 files changed

+43
-30
lines changed

2 files changed

+43
-30
lines changed

src/components/autocomplete/js/autocompleteController.js

+42-29
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ var ITEM_HEIGHT = 41,
77
MENU_PADDING = 8;
88

99
function MdAutocompleteCtrl ($scope, $element, $mdUtil, $mdConstant, $timeout, $mdTheming, $window,
10-
$animate, $rootElement, $attrs) {
10+
$animate, $rootElement, $attrs, $q) {
1111
//-- private variables
1212
var ctrl = this,
1313
itemParts = $scope.itemsExpr.split(/ in /i),
@@ -235,7 +235,9 @@ function MdAutocompleteCtrl ($scope, $element, $mdUtil, $mdConstant, $timeout, $
235235
*/
236236
function selectedItemChange (selectedItem, previousSelectedItem) {
237237
if (selectedItem) {
238-
$scope.searchText = getDisplayValue(selectedItem);
238+
getDisplayValue(selectedItem).then(function(val) {
239+
$scope.searchText = val;
240+
});
239241
}
240242
if (angular.isFunction($scope.itemChange) && selectedItem !== previousSelectedItem)
241243
$scope.itemChange(getItemScope(selectedItem));
@@ -283,21 +285,26 @@ function MdAutocompleteCtrl ($scope, $element, $mdUtil, $mdConstant, $timeout, $
283285
ctrl.index = getDefaultIndex();
284286
//-- do nothing on init
285287
if (searchText === previousSearchText) return;
286-
//-- clear selected item if search text no longer matches it
287-
if (searchText !== getDisplayValue($scope.selectedItem)) $scope.selectedItem = null;
288-
else return;
289-
//-- trigger change event if available
290-
if (angular.isFunction($scope.textChange) && searchText !== previousSearchText)
291-
$scope.textChange(getItemScope($scope.selectedItem));
292-
//-- cancel results if search text is not long enough
293-
if (!isMinLengthMet()) {
294-
ctrl.loading = false;
295-
ctrl.matches = [];
296-
ctrl.hidden = shouldHide();
297-
updateMessages();
298-
} else {
299-
handleQuery();
300-
}
288+
289+
getDisplayValue($scope.selectedItem).then(function(val) {
290+
//-- clear selected item if search text no longer matches it
291+
if (searchText !== val) $scope.selectedItem = null;
292+
else return;
293+
294+
//-- trigger change event if available
295+
if (angular.isFunction($scope.textChange) && searchText !== previousSearchText)
296+
$scope.textChange(getItemScope($scope.selectedItem));
297+
//-- cancel results if search text is not long enough
298+
if (!isMinLengthMet()) {
299+
ctrl.loading = false;
300+
ctrl.matches = [];
301+
ctrl.hidden = shouldHide();
302+
updateMessages();
303+
} else {
304+
handleQuery();
305+
}
306+
});
307+
301308
}
302309

303310
/**
@@ -371,7 +378,7 @@ function MdAutocompleteCtrl ($scope, $element, $mdUtil, $mdConstant, $timeout, $
371378
* @returns {*}
372379
*/
373380
function getDisplayValue (item) {
374-
return (item && $scope.itemText) ? $scope.itemText(getItemScope(item)) : item;
381+
return (item && $scope.itemText) ? $q.when($scope.itemText(getItemScope(item))) : $q.when(item);
375382
}
376383

377384
/**
@@ -441,20 +448,24 @@ function MdAutocompleteCtrl ($scope, $element, $mdUtil, $mdConstant, $timeout, $
441448
* Selects the item at the given index.
442449
* @param index
443450
*/
444-
function select (index) {
445-
$scope.selectedItem = ctrl.matches[index];
446-
ctrl.loading = false;
447-
ctrl.hidden = true;
448-
ctrl.index = 0;
449-
ctrl.matches = [];
451+
function select(index) {
450452
//-- force form to update state for validation
451-
$timeout(function () {
452-
elements.$.input.controller('ngModel').$setViewValue(getDisplayValue($scope.selectedItem) ||
453-
$scope.searchText);
454-
ctrl.hidden = true;
453+
$timeout(function() {
454+
getDisplayValue(ctrl.matches[index]).then(function(val) {
455+
var ngModel = elements.$.input.controller('ngModel');
456+
ngModel.$setViewValue(val);
457+
ngModel.$render();
458+
}).finally(function() {
459+
$scope.selectedItem = ctrl.matches[index];
460+
ctrl.loading = false;
461+
ctrl.hidden = true;
462+
ctrl.index = 0;
463+
ctrl.matches = [];
464+
});
455465
});
456466
}
457467

468+
458469
/**
459470
* Clears the searchText value and selected item.
460471
*/
@@ -501,7 +512,9 @@ function MdAutocompleteCtrl ($scope, $element, $mdUtil, $mdConstant, $timeout, $
501512
* Updates the ARIA messages
502513
*/
503514
function updateMessages () {
504-
ctrl.messages = [ getCountMessage(), getCurrentDisplayValue() ];
515+
getCurrentDisplayValue().then(function(msg) {
516+
ctrl.messages = [ getCountMessage(), msg ];
517+
});
505518
}
506519

507520
/**

src/components/autocomplete/js/autocompleteDirective.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ function MdAutocomplete ($mdTheming, $mdUtil) {
158158
class="md-visually-hidden"\
159159
role="status"\
160160
aria-live="assertive">\
161-
<p ng-repeat="message in $mdAutocompleteCtrl.messages" ng-if="message">{{message}}</p>\
161+
<p ng-repeat="message in $mdAutocompleteCtrl.messages track by $index" ng-if="message">{{message}}</p>\
162162
</aria-status>';
163163

164164
function getItemTemplate() {

0 commit comments

Comments
 (0)