Copied to clipboard

Flag this post as spam?

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


  • David Knittl 7 posts 107 karma points
    28 days ago
    David Knittl
    0

    Custom Property Editor with valueType JSON

    Hi there,

    I'm trying to implement a custom property editor in Umbraco 8. I know that the video on Umbraco.tv is outdated, but I still followed the instructions in

    https://umbraco.tv/videos/umbraco-v7/developer/extending/property-editors/storing-complex-data/

    and created my package manifest where I declared the valueType as JSON:

    {
        propertyEditors: [
            {
                alias: "MyEditor",
                name: "myEditor",
                editor: {
                    view: "~/App_Plugins/MyEditor/myEditor.editor.html",
                    valueType: "JSON"
                }
            }
        ]
    }
    

    In the view I try to bind two inputs to their corresponding properties (ng-model="model.value.val1" and ng-model="model.value.val2"):

    <input name="numberField1" class="umb-property-editor umb-number" type="number" ng-model="model.value.val1" />
    <input name="numberField2" class="umb-property-editor umb-number" type="number" ng-model="model.value.val2" />
    

    When I try to use this data type in the Umbraco backoffice, I get a Javascript error:

    Cannot create property 'val1' on string ''

    Does anyone know what changed in Umbraco v8 compared to v7 regarding custom property editors? Since the documentation is also not yet updated, I have no idea what's wrong here.

    Thanks in advance for any help you can provide!

    Cheers, David

  • David Knittl 7 posts 107 karma points
    19 days ago
    David Knittl
    0

    Nobody has any idea why this is?

  • Dennis Flæng Jørgensen 32 posts 115 karma points
    19 days ago
    Dennis Flæng Jørgensen
    100

    According to this post JSON is saved as ntext in the database. It might be because a string is the default value for that datatype. Im not sure if its an new issue in v8.

    What you can do as a fix is putting this in the controller for your property editor:

        var initModelValue = $scope.$watch('model.value', function (model) {
            if (typeof model === 'string' && model.length == 0)
                $scope.model.value = {};
            initModelValue(); //Deregisters the watch so we wont waste resources
        });
    
  • David Knittl 7 posts 107 karma points
    1 week ago
    David Knittl
    0

    Cool, thanks! That works flawlessly!

Please Sign in or register to post replies

Write your reply to:

Draft