From 452badab41580ca362e7fac017d2c2039116cd12 Mon Sep 17 00:00:00 2001 From: Shannon Date: Fri, 1 Feb 2019 11:27:40 +1100 Subject: [PATCH] Fixes tag editor for proper dirty tracking and model updating, the models weren't being updated correctly --- .../tags/umbtagseditor.directive.js | 37 ++++++++++++++----- .../propertyeditors/tags/tags.controller.js | 5 +-- 2 files changed, 28 insertions(+), 14 deletions(-) diff --git a/src/Umbraco.Web.UI.Client/src/common/directives/components/tags/umbtagseditor.directive.js b/src/Umbraco.Web.UI.Client/src/common/directives/components/tags/umbtagseditor.directive.js index 1850445bd4..4d10625d23 100644 --- a/src/Umbraco.Web.UI.Client/src/common/directives/components/tags/umbtagseditor.directive.js +++ b/src/Umbraco.Web.UI.Client/src/common/directives/components/tags/umbtagseditor.directive.js @@ -52,7 +52,8 @@ vm.isLoading = false; - configureViewModel(); + //ensure that the models are formatted correctly + configureViewModel(true); // Set the visible prompt to -1 to ensure it will not be visible vm.promptIsVisible = "-1"; @@ -123,9 +124,13 @@ }); } + /** + * Watch for value changes + * @param {any} changes + */ function onChanges(changes) { - // watch for value changes externally + //when the model 'value' changes, sync the viewModel object if (changes.value) { if (!changes.value.isFirstChange() && changes.value.currentValue !== changes.value.previousValue) { @@ -145,13 +150,15 @@ $element.find('.tags-' + vm.htmlId).typeahead('destroy'); } - function configureViewModel() { + function configureViewModel(isInitLoad) { if (vm.value) { if (angular.isString(vm.value) && vm.value.length > 0) { if (vm.config.storageType === "Json") { //json storage vm.viewModel = JSON.parse(vm.value); - updateModelValue(vm.viewModel); + if (!isInitLoad) { + updateModelValue(vm.viewModel); + } } else { //csv storage @@ -165,7 +172,10 @@ return self.indexOf(v) === i; }); - updateModelValue(vm.viewModel); + if (!isInitLoad) { + updateModelValue(vm.viewModel); + } + } } else if (angular.isArray(vm.value)) { @@ -175,13 +185,18 @@ } function updateModelValue(val) { - if (val) { - vm.onValueChanged({ value: val }); + + //need to format the underlying model value for persistence based on the storage type + if (vm.config.storageType === "Json") { + val = val ? val : []; } else { - vm.onValueChanged({ value: [] }); + //then it is csv and we need to format it like that + val = val ? val.join() : ""; } + vm.onValueChanged({ value: val }); + reValidate(); } @@ -273,8 +288,10 @@ } function reValidate() { - //this is required to re-validate - vm.tagEditorForm.tagCount.$setViewValue(vm.viewModel.length); + //this is required to re-validate for the mandatory validation + if (vm.tagEditorForm && vm.tagEditorForm.tagCount) { + vm.tagEditorForm.tagCount.$setViewValue(vm.viewModel.length); + } } } diff --git a/src/Umbraco.Web.UI.Client/src/views/propertyeditors/tags/tags.controller.js b/src/Umbraco.Web.UI.Client/src/views/propertyeditors/tags/tags.controller.js index a61930f877..688ac7693f 100644 --- a/src/Umbraco.Web.UI.Client/src/views/propertyeditors/tags/tags.controller.js +++ b/src/Umbraco.Web.UI.Client/src/views/propertyeditors/tags/tags.controller.js @@ -1,12 +1,9 @@ angular.module("umbraco") .controller("Umbraco.PropertyEditors.TagsController", - function ($scope, angularHelper) { + function ($scope) { $scope.valueChanged = function(value) { $scope.model.value = value; - // the model value seems to be a reference to the same array, so we need - // to set the form as dirty explicitly when the content of the array changes - angularHelper.getCurrentForm($scope).$setDirty(); } }