Skip to content
This repository has been archived by the owner on May 29, 2019. It is now read-only.

Commit

Permalink
feat(typeahead): support typeahead-loading bindable expression
Browse files Browse the repository at this point in the history
Closes #321
  • Loading branch information
pkozlowski-opensource committed Apr 13, 2013
1 parent 467afcd commit b58c9c8
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
15 changes: 15 additions & 0 deletions src/typeahead/test/typeahead.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,21 @@ describe('typeahead tests', function () {
changeInputValueTo(element, 'not in matches');
expect($scope.result).toEqual(undefined);
});

it('should bind loading indicator expression', inject(function ($timeout) {

$scope.isLoading = false;
$scope.loadMatches = function(viewValue) {
return $timeout(function() { return [];}, 1000);
};

var element = prepareInputEl("<div><input ng-model='result' typeahead='item for item in loadMatches()' typeahead-loading='isLoading'></div>");
changeInputValueTo(element, 'foo');

expect($scope.isLoading).toBeTruthy();
$timeout.flush();
expect($scope.isLoading).toBeFalsy();
}));
});

describe('selecting a match', function () {
Expand Down
11 changes: 9 additions & 2 deletions src/typeahead/typeahead.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ angular.module('ui.bootstrap.typeahead', [])
}])

//options - min length
.directive('typeahead', ['$compile', '$q', '$document', 'typeaheadParser', function ($compile, $q, $document, typeaheadParser) {
.directive('typeahead', ['$compile', '$parse', '$q', '$document', 'typeaheadParser', function ($compile, $parse, $q, $document, typeaheadParser) {

var HOT_KEYS = [9, 13, 27, 38, 40];

Expand All @@ -49,6 +49,8 @@ angular.module('ui.bootstrap.typeahead', [])
//should it restrict model values to the ones selected from the popup only?
var isEditable = originalScope.$eval(attrs.typeaheadEditable) !== false;

var isLoadingSetter = $parse(attrs.typeaheadLoading).assign || angular.noop;

//create a child scope for the typeahead directive so we are not polluting original scope
//with typeahead-specific data (matches, query etc.)
var scope = originalScope.$new();
Expand All @@ -64,6 +66,7 @@ angular.module('ui.bootstrap.typeahead', [])
var getMatchesAsync = function(inputValue) {

var locals = {$viewValue: inputValue};
isLoadingSetter(originalScope, true);
$q.when(parserResult.source(scope, locals)).then(function(matches) {

//it might happen that several async queries were in progress if a user were typing fast
Expand All @@ -88,8 +91,12 @@ angular.module('ui.bootstrap.typeahead', [])
} else {
resetMatches();
}
isLoadingSetter(originalScope, false);
}
}, resetMatches);
}, function(){
resetMatches();
isLoadingSetter(originalScope, false);
});
};

resetMatches();
Expand Down

0 comments on commit b58c9c8

Please sign in to comment.