cancel
Showing results for 
Search instead for 
Did you mean: 

SAPUI5 Issues with adding object as CustomData value

florin1335
Explorer
0 Kudos
187

Hi,

I need to assigned an object to a CustomData value and as specified in the documentation that's possible as long as not written to html. So, this seems to be working both in XML and JS:

<Input>
    <customData>
        <core:CustomData key="myCustomData" value="{ myProperty: 'myValue' }"/>
    </customData>
</Input>
new sap.m.Input({
    customData: [
        new sap.ui.core.CustomData({ key: "myCustomData", value: { myProperty: 'myValue' } }),
    ],
});

Result:

{
    "myCustomData": {
        "myProperty": "myValue"
    }
}

But, I am having issues due to conflicts with the binding related object property names, such as 'path'. So for example, if I replace 'myProperty' with 'path', the result is:

<Input>
    <customData>
        <core:CustomData key="myCustomData" value="{ path: 'myValue' }"/>
    </customData>
</Input>
{
    "myCustomData": null
}

For XML, it looks like I can overcome this by using the same approach as for the documented SmartTable p13nData, and escape the object so it's stringified:

<Input>
    <customData>
        <core:CustomData key="myCustomData" value='\{"path": "myValue"}'/>
    </customData>
</Input>
{
    "myCustomData": "{\"path\": \"myValue\"}"
}

But for JS, I cannot figure out how to do it to keep it a string:

new sap.m.Input({
    customData: [
        new sap.ui.core.CustomData({ key: "myCustomData", value: JSON.stringify({ path: 'myValue' }) }),
    ],
});
{
    "myCustomData": null
}

I am not sure whether doing this is bad or not and also afraid of other possible conflicts in the future (if my object property name becomes a binding relevant name).

View Entire Topic
junwu
Active Contributor
0 Kudos

this worked.

junwu_0-1725551278089.png

 

 

florin1335
Explorer
0 Kudos
That indeed seems to work, although the resulting string wouldn't be valid JSON, since that was the whole point, to turn it back into an object. Something like this will: '\\{"path":"NO"}'. I am not sure I get why though and what the parser is doing. Another thing I found in the source code was adding a 'ui5object' property on the object, which will not treat it as a binding and store the plain object, but unfortunately it doesn't seem to be public or documented.