From 10932fbb18b0887927ae71bb1f9e1d1d0f0f4e26 Mon Sep 17 00:00:00 2001 From: Matt Polichette Date: Tue, 9 Sep 2014 17:39:27 -0600 Subject: [PATCH] feat(autocomplete): Remove requirement for the source option to return 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 --- src/auto-complete.js | 4 ++-- test/auto-complete.spec.js | 10 ++++++++++ 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/src/auto-complete.js b/src/auto-complete.js index 2dd81a83..99ebf80b 100644 --- a/src/auto-complete.js +++ b/src/auto-complete.js @@ -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; @@ -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) { diff --git a/test/auto-complete.spec.js b/test/auto-complete.spec.js index 2c98c2c9..29e0b4fe 100644 --- a/test/auto-complete.spec.js +++ b/test/auto-complete.spec.js @@ -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' }]);