Hello folks,
This got me pulling the hair out of my head. I spent the last hour yelling at my screen while doing all sorts of tests and experiments.
I think I got it now, but... try it yourself.
Here's the json for a perspective view:
{
"custom": {},
"params": {
"category_id": "",
"family_id": "",
"item_id": ""
},
"propConfig": {
"custom.isFresh": {
"access": "PRIVATE"
},
"params.category_id": {
"paramDirection": "input",
"persistent": true
},
"params.family_id": {
"paramDirection": "input",
"persistent": true
},
"params.item_id": {
"paramDirection": "input",
"persistent": true
}
},
"props": {},
"root": {
"children": [
{
"meta": {
"name": "Label"
},
"position": {
"basis": "32px"
},
"props": {
"text": "open your console, type something in the text field, then click the button."
},
"type": "ia.display.label"
},
{
"meta": {
"name": "TextField"
},
"position": {
"basis": "32px"
},
"props": {
"placeholder": "type something here",
"text": "qwd"
},
"type": "ia.input.text-field"
},
{
"custom": {
"data": {
"text": "qwd"
}
},
"events": {
"component": {
"onActionPerformed": {
"config": {
"script": "\tdata \u003d self.custom.data\n\t\n\ttext \u003d self.getSibling(\"TextField\").props.text\n\tdata[\u0027text\u0027] \u003d text\n\t\n\tsystem.perspective.print(\u0027-\u0027*20)\n\tsystem.perspective.print(\"text: {}\".format(text))\n\tsystem.perspective.print(\"data[\u0027text\u0027]: {}\".format(data[\u0027text\u0027]))"
},
"scope": "G",
"type": "script"
}
}
},
"meta": {
"name": "Button"
},
"position": {
"basis": "34px"
},
"type": "ia.input.button"
},
{
"meta": {
"name": "Label_0"
},
"position": {
"basis": "32px"
},
"props": {
"text": "now check the code on the button..."
},
"type": "ia.display.label"
}
],
"meta": {
"name": "root"
},
"props": {
"direction": "column",
"style": {
"gap": "10px",
"padding": "15px"
}
},
"type": "ia.container.flex"
}
}
Here's a quick summary:
There's a button. There's a data
custom property.
The button has code on onActionPerformed
as follow:
data = self.custom.data
text = self.getSibling("TextField").props.text
data['text'] = text
system.perspective.print('-'*20)
system.perspective.print("text: {}".format(text))
system.perspective.print("data['text']: {}".format(data['text']))
The text field is only there to provide a way to change the value
Simple, right ? wrong:
I'm guessing the printing happens before the property actually gets its value and since data
is just a reference to that property, the old value is the one that gets printed.
It would make sense, but damn did it trip me up !
If it's not that, then I'll take the whatever explanation you might have.
Anyway, if you ever find yourself in this situation - keep calm, declare another variable and use this one instead.