From 6a830116f25bcedac37dc84b457dff3118a4f4e7 Mon Sep 17 00:00:00 2001 From: Michal Charemza Date: Fri, 7 Mar 2014 16:34:32 +0000 Subject: [PATCH] fix(typeahead): loading callback updates after blur Fixes #1822 Closes #1904 --- src/typeahead/test/typeahead.spec.js | 19 +++++++++++++++++++ src/typeahead/typeahead.js | 5 ++++- 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/src/typeahead/test/typeahead.spec.js b/src/typeahead/test/typeahead.spec.js index 1e2ec60b68..59f3f661aa 100644 --- a/src/typeahead/test/typeahead.spec.js +++ b/src/typeahead/test/typeahead.spec.js @@ -507,6 +507,25 @@ describe('typeahead tests', function () { expect(element).toBeClosed(); }); + it('should properly update loading callback if an element is not focused', function () { + + $scope.items = function(viewValue) { + return $timeout(function(){ + return [viewValue]; + }); + }; + var element = prepareInputEl('
'); + var inputEl = findInput(element); + + changeInputValueTo(element, 'match'); + $scope.$digest(); + + inputEl.blur(); + $timeout.flush(); + + expect($scope.isLoading).toBeFalsy(); + }); + it('issue 1140 - should properly update loading callback when deleting characters', function () { $scope.items = function(viewValue) { diff --git a/src/typeahead/typeahead.js b/src/typeahead/typeahead.js index e0cb5b9f6a..7148ce3031 100644 --- a/src/typeahead/typeahead.js +++ b/src/typeahead/typeahead.js @@ -110,7 +110,8 @@ angular.module('ui.bootstrap.typeahead', ['ui.bootstrap.position', 'ui.bootstrap //it might happen that several async queries were in progress if a user were typing fast //but we are interested only in responses that correspond to the current view value - if (inputValue === modelCtrl.$viewValue && hasFocus) { + var onCurrentRequest = (inputValue === modelCtrl.$viewValue); + if (onCurrentRequest && hasFocus) { if (matches.length > 0) { scope.activeIdx = 0; @@ -136,6 +137,8 @@ angular.module('ui.bootstrap.typeahead', ['ui.bootstrap.position', 'ui.bootstrap } else { resetMatches(); } + } + if (onCurrentRequest) { isLoadingSetter(originalScope, false); } }, function(){