Refactor tragsWithTranslation directive to handle dynamic attr names

This commit is contained in:
Rob Harrington
2016-03-18 11:20:32 +11:00
parent f70b1f4572
commit 47df106237

View File

@@ -1,8 +1,15 @@
angular.module("admin.utils").directive "tagsWithTranslation", ->
angular.module("admin.utils").directive "tagsWithTranslation", ($timeout) ->
restrict: "E"
template: "<tags-input ng-model='object.tags'>"
template: "<tags-input ng-model='object[tagsAttr]'>"
scope:
object: "="
tagsAttr: "@?"
tagListAttr: "@?"
link: (scope, element, attrs) ->
scope.$watchCollection "object.tags", ->
scope.object.tag_list = (tag.text for tag in scope.object.tags).join(",")
$timeout ->
scope.tagsAttr ||= "tags"
scope.tagListAttr ||= "tag_list"
watchString = "object.#{scope.tagsAttr}"
scope.$watchCollection watchString, ->
scope.object[scope.tagListAttr] = (tag.text for tag in scope.object[scope.tagsAttr]).join(",")