'use strict'; /** * Tag selection directive. */ App.directive('selectTag', function() { return { restrict: 'E', templateUrl: 'partial/directive.selecttag.html', replace: true, scope: { tags: '=', ref: '@' }, controller: function($scope, Tag) { // Retrieve tags Tag.tags().then(function(data) { $scope.allTags = data.tags; }); /** * Add a tag. */ $scope.addTag = function($event) { // Does the new tag exists var tag = _.find($scope.allTags, function(tag) { if (tag.name == $scope.input) { return tag; } }); // Does the new tag is already in the model var duplicate = _.find($scope.tags, function(tag2) { if (tag && tag2.id == tag.id) { return tag2; } }); // Add the new tag if (tag) { if (!duplicate) { $scope.tags.push(tag); } $scope.input = ''; } if ($event) { $event.preventDefault(); } } /** * Remove a tag. */ $scope.deleteTag = function(deleteTag) { $scope.tags = _.reject($scope.tags, function(tag) { return tag.id == deleteTag.id; }) }; }, link: function(scope, element, attr, ctrl) { } } });