From 47df106237cb9aa593d473ead7c199833d4ebfc2 Mon Sep 17 00:00:00 2001 From: Rob Harrington Date: Fri, 18 Mar 2016 11:20:32 +1100 Subject: [PATCH] Refactor tragsWithTranslation directive to handle dynamic attr names --- .../directives/tags_with_translation.js.coffee | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/app/assets/javascripts/admin/utils/directives/tags_with_translation.js.coffee b/app/assets/javascripts/admin/utils/directives/tags_with_translation.js.coffee index eae1a899c1..6ce7953608 100644 --- a/app/assets/javascripts/admin/utils/directives/tags_with_translation.js.coffee +++ b/app/assets/javascripts/admin/utils/directives/tags_with_translation.js.coffee @@ -1,8 +1,15 @@ -angular.module("admin.utils").directive "tagsWithTranslation", -> +angular.module("admin.utils").directive "tagsWithTranslation", ($timeout) -> restrict: "E" - template: "" + template: "" 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(",")