Problem with instance.data.variable not working

Hello.

I’m really driving crazy with this problem.

My update has properties.xxx (from the element fields).
Everything ok.

I want to pass some variables to my element action, so I did this:

instance.data.source = properties.source;
instance.data.color = properties.getcolor;
console.log(instance.data.source);
console.log(instance.data.color);

result:
https://xx.com
#000FFF

Easy right?

Ok now in my element action code:

var source = instance.data.source;
var color = instance.data.color;

console.log(source);
console.log(color);

result:
undefined
undefined

What i’m missing ??

Are you passing between elements in the same plugin?

Or are you trying to pass data to/from a workflow action from an element action?

Something like that?

I do this ^^^^^ passing of data all the time

Hi @jared.gibb

Yes it’s in the same plugin. Same element (My plugin only has one element).

As said, im trying to pass data from update to element action.

But for some reason is not working…

It feels like the data aren’t getting where you think they should be. In update, keep the console log. Only after you see it fire in update, try to run your action and see the result. Remove all code except for the passing of data and logging to check if it’s there.

If that doesn’t work the issue would likely lie in the update code and something blocking those data from passing.

Did you get that sorted out?

I have the same problem


24-08-2021 10-44-35

The only real possibility is that the Action is running prior to the Update function, or prior to Update running once the data you are pulling in via Properties is available to it.

You could bring the Properties that you need directly in via the Action, instead of relying on Update? There may be a reason that you are not doing that…

If you have to use Update for some reason then you could do something like:

  1. Set instance.data.init = false In your Initialize function
  2. Within Update Check for values within your Properties object whilst instance.data.init = false
  3. Once you find values then set instance.data.init to true And trigger an event (instance.triggerEvent()) that then, via a workflow, calls your Action.

I often use this kind of switch to better manage how plugins initialise because the sequence of operation during page load is unpredictable and Update will often run several times during one page load.

Thanks! You’re right.

After setting the 1s timeout in action, everything worked
24-08-2021 12-27-53

I am planning to create my own global object and store the necessary variables in it.

1 Like

Is that the only solution?

I think you’re solving for the following:

  • initialize will always run before update - neither will run when the element is hidden
  • actions may run before update has had the opportunity to, therefore you can’t rely on update to bring in data that your actions may rely on.
  • actions won’t run before initialize has run and can therefore reference data and methods that were defined inside initialize, as long as your element is not hidden.
  • but if it is hidden, actions can still run but you can no longer rely on data or methods that were defined within initialize

So you’re writing your code to reliably work within those constraints - I guess anything that does that can be considered your solution.

2 Likes

Thanks Edward,
You pointed me in the right direction.
The element was shown in the editor, but still the ‘initialize’ function wasn’t triggered because I didn’t mark this option:
“Support standard visible property”
If you don’t enable it, the initialize function never runs.

1 Like