             addValue(target.val())
             target.val("")
             $el.find("input").autocomplete("close")
-
 
         $el.on "click", ".icon-delete", (event) ->
             event.preventDefault()


    link = ($scope, $el, $attrs, $model) ->
        editable = if $attrs.editable == "true" then true else false
        $el.addClass('tags-block')

        addValue = (value) ->
            value = trim(value)

            $scope.$apply ->
                $model.$setViewValue(normalizeTags(tags))

        $scope.$watch $attrs.ngModel, (val) ->
            console.log "WATCH", val
            renderTags($el, val, editable, $scope.project.tags_colors) if val?

        bindOnce $scope, "projectId", (projectId) ->
            # If not editable, no tags preloading is needed.
            return if not editable

            positioningFunction = (position, elements) ->
                menu = elements.element.element
                menu.css("width", elements.target.width)
                menu.css("top", position.top)
                menu.css("left", position.left)

            $rs.projects.tags(projectId).then (data) ->
                $el.find("input").autocomplete({
                    source: data
                    position: {
                        my: "left top",
                        using: positioningFunction
                    }
                    select: (event, ui) ->
                        addValue(ui.item.value)
                        ui.item.value = ""
                })

        if not editable
            $el.find("input").remove()

        $el.on "keypress", "input", (event) ->
            return if event.keyCode != 13
