Custom widget - How to get type from attribute JSON?

1
Hi there! I’m working on a custom widget, and I can’t for the life of me figure out how to detect the type of attribute that has been passed to it. A feature of this widget is that you can select any attribute type, but it’s almost necessary that I can validate that it matches another attribute’s type that is passed to the widget. Essentially, I have two attributes that can be passed to this widget, `presetAttribute` and `targetAttribute`, and I want to make sure that they are of the same type. `presetAttribute` connects to a datasource on the widget. This is important to note, because it actually returns its value type. However, `targetAttribute` does not. This is what the breakdown looks like in the console. As you can see, I can detect the type from `presetAttribute`, but there seems to be no way to get the type of attribute from `targetAttribute`. I have already tested with several different data types, but they each have their own structure and simply do not give me a consistent way to get this information. Can anyone give me a direction, or confirm that there is simply no way for me to do this?
asked
1 answers
0

Hopefully Mendix adds this feature at some point.

 

In the meantime, I found this very ugly workaround:

validateAttributeTypes() {
    // Get current value and a test value from the list of presets
    let trueValue = this.props.targetAttribute.value;
    let testValue = this.props.presetAttribute.get(
        this.props.presetObject.items[0]
    ).value

    let valid = true;

    try {
        this.props.targetAttribute.setValue(testValue);                 // Attempt to set our target attribute to a preset
        console.log("Preset and target attribute types are matching");
    } catch (e) {                                                       // If it fails, it must not be of the right type
        alert(`\n${e}\n\nPreset Selector preset and target attributes must be of the same type.`)
        valid = false;
   }

    this.props.targetAttribute.setValue(trueValue); // Set targetAttribute back to the right value

    return valid;
}

Essentially, you can try to set the value of the attribute in question and Mendix will either respond with nothing or throw an error telling you that the types aren’t compatible.

answered