diff --git a/src/components/autocomplete/autocomplete.spec.js b/src/components/autocomplete/autocomplete.spec.js index cf72bf9aa82..c71bf1fbeb0 100644 --- a/src/components/autocomplete/autocomplete.spec.js +++ b/src/components/autocomplete/autocomplete.spec.js @@ -477,6 +477,75 @@ describe('', function() { expect(ctrl2.hasNotFound).toBe(false); })); + it('should even show the md-not-found template if we have lost focus', inject(function($timeout) { + var scope = createScope(); + var template = + '' + + ' {{item.display}}' + + ' Sorry, not found...' + + ''; + + var element = compile(template, scope); + var controller = element.controller('mdAutocomplete'); + + controller.focus(); + + scope.searchText = 'somethingthatdoesnotexist'; + + $timeout.flush(); + + controller.listEnter(); + expect(controller.notFoundVisible()).toBe(true); + + controller.blur(); + expect(controller.notFoundVisible()).toBe(true); + + controller.listLeave(); + expect(controller.notFoundVisible()).toBe(false); + + $timeout.flush(); + element.remove(); + + })); + + it('should not show the md-not-found template if we lost focus and left the list', inject(function($timeout) { + var scope = createScope(); + var template = + '' + + ' {{item.display}}' + + ' Sorry, not found...' + + ''; + + var element = compile(template, scope); + var controller = element.controller('mdAutocomplete'); + + controller.focus(); + + scope.searchText = 'somethingthatdoesnotexist'; + + $timeout.flush(); + + controller.listEnter(); + expect(controller.notFoundVisible()).toBe(true); + + controller.listLeave(); + controller.blur(); + expect(controller.notFoundVisible()).toBe(false); + + $timeout.flush(); + element.remove(); + })); + }); describe('xss prevention', function() { diff --git a/src/components/autocomplete/js/autocompleteController.js b/src/components/autocomplete/js/autocompleteController.js index e82d092b780..dd3b51df95d 100644 --- a/src/components/autocomplete/js/autocompleteController.js +++ b/src/components/autocomplete/js/autocompleteController.js @@ -18,7 +18,8 @@ function MdAutocompleteCtrl ($scope, $element, $mdUtil, $mdConstant, $mdTheming, noBlur = false, selectedItemWatchers = [], hasFocus = false, - lastCount = 0; + lastCount = 0, + promiseFetch = false; //-- public variables with handlers defineProperty('hidden', handleHiddenChange, true); @@ -638,11 +639,13 @@ function MdAutocompleteCtrl ($scope, $element, $mdUtil, $mdConstant, $mdTheming, handleResults(items); } else if (items) { setLoading(true); + promiseFetch = true; $mdUtil.nextTick(function () { if (items.success) items.success(handleResults); if (items.then) items.then(handleResults); if (items.finally) items.finally(function () { setLoading(false); + promiseFetch = false; }); },true, $scope); } @@ -707,7 +710,7 @@ function MdAutocompleteCtrl ($scope, $element, $mdUtil, $mdConstant, $mdTheming, function notFoundVisible () { var textLength = (ctrl.scope.searchText || '').length; - return ctrl.hasNotFound && !hasMatches() && !ctrl.loading && textLength >= getMinLength() && hasFocus && !hasSelection(); + return ctrl.hasNotFound && !hasMatches() && (!ctrl.loading || promiseFetch) && textLength >= getMinLength() && (hasFocus || noBlur) && !hasSelection(); } /**