From 9bcd9d67aeca933c6f3f1aee666818a899e6df25 Mon Sep 17 00:00:00 2001 From: Nicholas-Westby Date: Mon, 4 Sep 2017 15:49:30 -0700 Subject: [PATCH] Update the Archetype controller to allow for fieldset titles to be loaded using promises (still need to actually allow for promises). --- app/controllers/controller.js | 55 ++++++++++++++++++++++++++- app/services/archetypeLabelService.js | 5 ++- 2 files changed, 57 insertions(+), 3 deletions(-) diff --git a/app/controllers/controller.js b/app/controllers/controller.js index 2f538f7..e3ea816 100644 --- a/app/controllers/controller.js +++ b/app/controllers/controller.js @@ -42,8 +42,53 @@ angular.module("umbraco").controller("Imulus.ArchetypeController", function ($sc } //helper to get $eval the labelTemplate + $scope.fieldsetTitles = []; $scope.getFieldsetTitle = function (fieldsetConfigModel, fieldsetIndex) { - return archetypeLabelService.getFieldsetTitle($scope, fieldsetConfigModel, fieldsetIndex); + + // Ensure the collection of titles is large enough. + ensureEnoughTitles(fieldsetIndex + 1); + var title = $scope.fieldsetTitles[fieldsetIndex]; + + // Return the title if it's already been loaded. + if (title.loaded) { + return title.value; + } + + // Return early if the title is still loading. + if (title.loading) { + return; + } + + // Start loading the title. + title.loading = true; + title.loaded = false; + title.value = null; + archetypeLabelService.getFieldsetTitle($scope, fieldsetConfigModel, fieldsetIndex) + .then(function(value) { + + // Finished loading the title. + title.loaded = true; + title.loading = false; + title.value = value; + + }); + + // Still loading a title, so do not return a title. + + }; + + /** + * Ensure the collection of fieldset titles is large enough to accommodate the number of fieldsets. + * @param count The number of fieldsets. + */ + function ensureEnoughTitles(count) { + while ($scope.fieldsetTitles.length < count) { + $scope.fieldsetTitles.push({ + loading: false, + loaded: false, + value: null + }); + } } //sort config @@ -697,6 +742,14 @@ angular.module("umbraco").controller("Imulus.ArchetypeController", function ($sc fileManager.setFiles($scope.model.alias, archetypeFiles); } + // Deep watch for changes. + $scope.$watch('model.value', function () { + + // Empty titles. If the value has changed, the titles may have changed. + $scope.fieldsetTitles = []; + + }, true); + //watch for changes $scope.$watch('model.value', function (v) { if ($scope.model.config.developerMode) { diff --git a/app/services/archetypeLabelService.js b/app/services/archetypeLabelService.js index 0756b8e..89ab687 100644 --- a/app/services/archetypeLabelService.js +++ b/app/services/archetypeLabelService.js @@ -1,4 +1,4 @@ -angular.module('umbraco.services').factory('archetypeLabelService', function (archetypeCacheService) { +angular.module('umbraco.services').factory('archetypeLabelService', function (archetypeCacheService, $q) { //private function executeFunctionByName(functionName, context) { @@ -353,7 +353,8 @@ angular.module('umbraco.services').factory('archetypeLabelService', function (ar parsedTemplate = parsedTemplate.replace(match, templateLabelValue); }); - return parsedTemplate; + // Wrap the title in a promise. + return $q.when(parsedTemplate); } } }); \ No newline at end of file