From f5a3fbcba8ee175ad69710b16471f0493c69e350 Mon Sep 17 00:00:00 2001 From: Jeff Treuting Date: Mon, 6 Oct 2014 16:12:16 -0700 Subject: [PATCH] feat(typeahead): add noResults indicator binding --- src/typeahead/test/typeahead.spec.js | 34 ++++++++++++++++++++++++++++ src/typeahead/typeahead.js | 8 ++++++- 2 files changed, 41 insertions(+), 1 deletion(-) diff --git a/src/typeahead/test/typeahead.spec.js b/src/typeahead/test/typeahead.spec.js index de9a385d3f..2445e4943c 100644 --- a/src/typeahead/test/typeahead.spec.js +++ b/src/typeahead/test/typeahead.spec.js @@ -384,6 +384,40 @@ describe('typeahead tests', function () { expect($scope.result).toEqual('AL'); expect(inputEl.val()).toEqual('AL'); }); + + it('should bind no results indicator as true when no matches returned', inject(function ($timeout) { + + $scope.isNoResults = false; + $scope.loadMatches = function (viewValue) { + return $timeout(function () { + return []; + }, 1000); + }; + + var element = prepareInputEl('
'); + changeInputValueTo(element, 'foo'); + + expect($scope.isNoResults).toBeFalsy(); + $timeout.flush(); + expect($scope.isNoResults).toBeTruthy(); + })); + + it('should bind no results indicator as false when matches are returned', inject(function ($timeout) { + + $scope.isNoResults = false; + $scope.loadMatches = function (viewValue) { + return $timeout(function () { + return [viewValue]; + }, 1000); + }; + + var element = prepareInputEl('
'); + changeInputValueTo(element, 'foo'); + + expect($scope.isNoResults).toBeFalsy(); + $timeout.flush(); + expect($scope.isNoResults).toBeFalsy(); + })); }); describe('pop-up interaction', function () { diff --git a/src/typeahead/typeahead.js b/src/typeahead/typeahead.js index f8babc7c56..e9c2ee6251 100644 --- a/src/typeahead/typeahead.js +++ b/src/typeahead/typeahead.js @@ -55,6 +55,9 @@ angular.module('ui.bootstrap.typeahead', ['ui.bootstrap.position', 'ui.bootstrap //a callback executed when a match is selected var onSelectCallback = $parse(attrs.typeaheadOnSelect); + //binding to a variable that indicates if there were no results after the query is completed + var isNoResultsSetter = $parse(attrs.typeaheadNoResults).assign || angular.noop; + var inputFormatter = attrs.typeaheadInputFormatter ? $parse(attrs.typeaheadInputFormatter) : undefined; var appendToBody = attrs.typeaheadAppendToBody ? originalScope.$eval(attrs.typeaheadAppendToBody) : false; @@ -123,6 +126,7 @@ angular.module('ui.bootstrap.typeahead', ['ui.bootstrap.position', 'ui.bootstrap var locals = {$viewValue: inputValue}; isLoadingSetter(originalScope, true); + isNoResultsSetter(originalScope, false); $q.when(parserResult.source(originalScope, locals)).then(function(matches) { //it might happen that several async queries were in progress if a user were typing fast @@ -130,7 +134,7 @@ angular.module('ui.bootstrap.typeahead', ['ui.bootstrap.position', 'ui.bootstrap var onCurrentRequest = (inputValue === modelCtrl.$viewValue); if (onCurrentRequest && hasFocus) { if (matches.length > 0) { - + isNoResultsSetter(originalScope, false); scope.activeIdx = 0; scope.matches.length = 0; @@ -154,6 +158,7 @@ angular.module('ui.bootstrap.typeahead', ['ui.bootstrap.position', 'ui.bootstrap element.attr('aria-expanded', true); } else { resetMatches(); + isNoResultsSetter(originalScope, true); } } if (onCurrentRequest) { @@ -162,6 +167,7 @@ angular.module('ui.bootstrap.typeahead', ['ui.bootstrap.position', 'ui.bootstrap }, function(){ resetMatches(); isLoadingSetter(originalScope, false); + isNoResultsSetter(originalScope, true); }); };