Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Simon Dingley 1474 posts 3431 karma points c-trib
    Jun 11, 2024 @ 11:14
    Simon Dingley
    0

    Custom Datatype - Accessing data from other config values

    In Umbraco v12 I am trying to create a custom datatype. I have 2 config properties one called items and the other called defaultValue. In angular how can I access the data in items from defaultValue in order to populate a dropdown.

    Here is my controller for the defaultValue config editor:

    angular.module("umbraco").controller("RadioButtonListWithDefault.Controller", ['$scope', '$controller', function ($scope, $controller) {
          // Extend the core CheckboxListController
          angular.extend(this, $controller('Umbraco.PropertyEditors.CheckboxListController', { $scope: $scope }));
    
          const vm = this;
          vm.model = $scope.model;
    
          // Initialize items and default from the configuration
          vm.items = vm.model.config.items;
          vm.default = vm.model.config.default;
    
          // Watch for changes in the items configuration and react accordingly
          $scope.$watch('vm.model.config.items', function (newValue, oldValue) {
              if (newValue !== oldValue) {
                  vm.items = newValue;
                  updateDefaultItemOptions();
              }
          }, true);
    
          // Watch for changes in the default configuration
          $scope.$watch('vm.model.config.default', function (newValue, oldValue) {
              if (newValue !== oldValue) {
                  vm.default = newValue;
              }
          }, true);
    
          // Function to update default item options based on the items property
          function updateDefaultItemOptions() {
              if (vm.items) {
                  vm.defaultItemOptions = vm.items.map(item => ({
                      value: item.value,
                      label: item.value
                  }));
              }
          }
    
          // Initialize default item options
          updateDefaultItemOptions();
    
          // Ensure model value is set to default if not already set
          if (!vm.model.value && vm.default) {
              vm.model.value = vm.default;
          }
    
          console.log("Model:", vm.model); 
          console.log("Items:", vm.items); 
          console.log("Default:", vm.default);
          console.log("Default Item Options:", vm.defaultItemOptions); // Debugging line
        }]);
    

    $scope.model only gives me access to the current config item model and not the datatype config so that I can access the items.

Please Sign in or register to post replies

Write your reply to:

Draft