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

Commit

Permalink
fix(macTagAutocomplete): Fixed source not getting set properly compil…
Browse files Browse the repository at this point in the history
…ing on top of another directive
  • Loading branch information
adrianlee44 committed Jan 21, 2014
1 parent 27e4a85 commit b7ac7af
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 7 deletions.
21 changes: 14 additions & 7 deletions src/directives/tag_autocomplete.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
@name Tag Autocomplete
@description
A directive for generating tag input with autocomplete support on text input
A directive for generating tag input with autocomplete support on text input.
Tag autocomplete has priority 800
@dependencies
- mac-autocomplete
Expand Down Expand Up @@ -49,6 +50,7 @@ angular.module("Mac").directive "macTagAutocomplete", [
restrict: "E"
templateUrl: "template/tag_autocomplete.html"
replace: true
priority: 800
scope:
url: "=macTagAutocompleteUrl"
placeholder: "=macTagAutocompletePlaceholder"
Expand Down Expand Up @@ -86,9 +88,8 @@ angular.module("Mac").directive "macTagAutocomplete", [
($scope, element, attrs) ->
# Variable for input element
$scope.textInput = ""
if useSource
$scope.autocompleteSource =
if angular.isArray($scope.source) then [] else $scope.source
$scope.autocompleteSource =
if angular.isArray($scope.source) then [] else $scope.source

# NOTE: Proxy is created to prevent tag autocomplete from breaking
# when user did not specify model
Expand Down Expand Up @@ -129,16 +130,22 @@ angular.module("Mac").directive "macTagAutocomplete", [
$scope.autocompletePlaceholder =
if $scope.selected?.length then "" else $scope.placeholder

return unless useSource and angular.isArray($scope.source)
unless useSource and angular.isArray($scope.source)
$scope.autocompleteSource = $scope.source
return

sourceValues = (item[valueKey] for item in ($scope.source or []))
selectedValues = (item[valueKey] for item in ($scope.selected or []))
difference = (item for item in sourceValues when item not in selectedValues)

$scope.autocompleteSource =
(item for item in ($scope.source or []) when item[valueKey] in difference)

if useSource and angular.isArray($scope.source)
$scope.$watchCollection "source", updateAutocompleteSource
if useSource
# NOTE: Watcher on source is added for string and function type to make sure
# scope value is copied correctly into this scope
watchFn = if angular.isArray($scope.source) then "$watchCollection" else "$watch"
$scope[watchFn] "source", updateAutocompleteSource

$scope.$watchCollection "selected", updateAutocompleteSource

Expand Down
8 changes: 8 additions & 0 deletions test/unit/tag_autocomplete.spec.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,14 @@ describe "Mac tag autocomplete", ->
textInput = $(".mac-autocomplete", element)
expect(textInput.attr("mac-autocomplete-source")).toBe "autocompleteSource"

it "should have the same source function as the parent scope", ->
$rootScope.source = jasmine.createSpy "source"
element = $compile("<mac-tag-autocomplete mac-tag-autocomplete-source='source'></mac-tag-autocomplete>") $rootScope
$rootScope.$digest()
$rootScope.$$childHead.autocompleteSource()

expect($rootScope.source).toHaveBeenCalled()

xit "should focus on autocomplete when click on tag autocomplete", ->
$rootScope.source = ["test", "test1", "test2"]
element = $compile("<mac-tag-autocomplete mac-tag-autocomplete-source='source'></mac-tag-autocomplete>") $rootScope
Expand Down

0 comments on commit b7ac7af

Please sign in to comment.