[Model SDK] How to add new WidgetObject to WidgetValue of a custom widgets WidgetProperty?

0
I am currently working on a project with the Mendix Model SDK. Moreover, I am trying to modify the properties of an existing custom widget on the working copy. With this post, I would like to explain and inquire about an issue with modifying custom widget properties.   Desired outcome Consider example 1 below. In this instance, the custom widget has a property captioned as ‘For label field’. The value of the property consists of a collection of widget objects. Using the model SDK, I would like to add a new entry to this list, such that the new state looks similar to Example 2. Example 1: properties overview of a custom widget. Example 2: desired output after adding a new widget object to the ‘For label field’ property value with the SDK. Implementation Attempts In the descriptions below, we already have access to the custom widget, and its ‘For label field’ captioned property already contains a widget object entry similar to example 1.   customwidgets.WidgetObject.deepCopy For starters, I have tried deepcopying the existing customwidgets.WidgetObject {‘Placeholder help text’, ‘Placeholder label id’, Placeholder label’}, which is stored in ‘my_widget.properties[2].value.objects’. let val = (this.helpText.properties[2].value as customwidgets.WidgetValue); let objs = val.objects; Unfortunately, with variables above, in seperate tryouts, callling ‘objs.push(objs[0].deepCopy(val.model))’ or ‘objs.push(objs[0].deepCopy(objs[0].model))’ both throw the following exception (for readability, element ID was omitted): Error: By-id referred element CustomWidgets$WidgetObjectType with ID <some-uuid> was not included in the deepCopy It seems like the SDK’s deepcopy method was not able to the correctly copy the widgetObjectType. Intuitively, speculatively, I would think the SDK does not know what kind of custom widget it is copying, and errors.   customwidgets.WidgetObject.createInWidgetValueUnderObjects(val) In a second attempt, using the method createWidgetValueUnderObjects() does not yield any errors in the sdk. However, when opening mendix, the following error occurs when trying to access the custom widget’s properties. Similar error occurs when creating the object and pushing to the objects list using ‘obs.push(customwidgets.WidgetObject.create(val.model))’       How to add new WidgetObject to WidgetValue of a custom widgets WidgetProperty? Am I missing some metadata that I should provide to the SDK, or is there something else I am doing wrong?   Much thanks for reading along, Lars      
asked
1 answers
1

Hey Lars,

First off… creating custom widget objects with the SDK is very tricky. This comes from the fact that this part of the model (especially the `CustomWidgetType` and contents) represent a separate model inside the Mendix model. The SDK does not offer many safety nets here, and – as you already found – might cause problems in Pro when not set up completely.

However, it should be doable. I’ll try to cover all parts (and hope I don’t miss anything, given the complexity of this).

 

To begin, you’ll need the `WidgetObjectType` that belongs to your object property; assuming you already have the corresponding `WidgetProperty` as `property`, you should be able to retrieve that through `property.type.valueType.objectType`.

To create the new object, it’s best to build it fully before adding it to the list, so using `customwidgets.WidgetObject.create(model)` is preferred. On the resulting object, set the `type` field to the `objectType` from the previous step.

However, now comes the tricky part; as this `WidgetObject` will need to be initialized with all properties and corresponding values. To do so, loop over all properties from the `objectType.propertyTypes`.

For each of those `propertyType` results; create a `customwidgets.WidgetProperty` (in the same way as the object; using `.create(model)`) and set the `type` to the `propertyType`. Next, create a `customwidgets.WidgetValue` (once again, using `.create(model)` and set it’s `type` to `propertyType.valueType`. This value can now be set on the `value` field of the created property.

Finally, you could/should set the value’s value. Which field in `WidgetValue` is correct depends on the `WidgetValueType` inside the `valueType.type` field.

Note:

- `TranslatableString` uses the `translatableValue` field, which Studio Pro might actually expect to be set to a value… so you might need to create a `texts.Text` instance in this field even if you don’t want to set anything for this property type.

- `TextTemplate` uses the `template` field; which probably needs to be set as well for Pro.

- `Action` uses `action`, but that field seems to have a default value at least (being `NoClientAction`), so that should be fine for Pro by default.

All other properties should, I believe, work with whatever the SDK puts in the fields by default.

 

(edit, I forgot to mention this:) As the last thing; you’ll of course need to add the created object to the `objects` list in the initial widget value

 

Once again; this whole process is pretty tricky and error-prone. Here be dragons; make sure to back up the project, as it might break in a way that can only be (attempted to) fix in the SDK :).

 

Best of luck!

answered