Skip to content
This repository has been archived by the owner on Nov 22, 2021. It is now read-only.

Commit

Permalink
feat(autocomplete): Remove requirement for the source option to retur…
Browse files Browse the repository at this point in the history
…n a promise

Use $q.when to remove api restriction that the result of the source option
must be a promise. This is a simple non-breaking addition which makes the
API more simple to use.

Closes #237
  • Loading branch information
Matt Polichette authored and mbenford committed Nov 30, 2014
1 parent c5235be commit 10932fb
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/auto-complete.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
* @param {boolean=} {loadOnFocus=false} Flag indicating that the source option will be evaluated when the input element
* gains focus. The current input value is available as $query.
*/
tagsInput.directive('autoComplete', function($document, $timeout, $sce, tagsInputConfig) {
tagsInput.directive('autoComplete', function($document, $timeout, $sce, $q, tagsInputConfig) {
function SuggestionList(loadFn, options) {
var self = {}, debouncedLoadId, getDifference, lastPromise;

Expand Down Expand Up @@ -56,7 +56,7 @@ tagsInput.directive('autoComplete', function($document, $timeout, $sce, tagsInpu
debouncedLoadId = $timeout(function() {
self.query = query;

var promise = loadFn({ $query: query });
var promise = $q.when(loadFn({ $query: query }));
lastPromise = promise;

promise.then(function(items) {
Expand Down
10 changes: 10 additions & 0 deletions test/auto-complete.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,16 @@ describe('autoComplete directive', function() {
expect(isSuggestionsBoxVisible()).toBe(false);
});

it('loads values from the load function even if the return value is not a promise', function() {
// Arrange
$scope.loadItems = jasmine.createSpy().and.returnValue(generateSuggestions(3));
// Act
suggestionList.load('', []);
$timeout.flush();
// Assert
expect(getSuggestions().length).toBe(3);
});

it('renders all elements returned by the load function that aren\'t already added', function() {
// Act
tagsInput.getTags.and.returnValue([{ text: 'Item3' }]);
Expand Down

0 comments on commit 10932fb

Please sign in to comment.