Getting element properties during initialisation

Yes, I know this is a bugbear to most plugin developers, and frankly - I’m surprised there isn’t more of an uproar about the lack of this most basic of all basic functions. But I am curious - has anyone worked out a way to achieve this?

I have a plugin that retrieves data, and has a configurable boolean input (dynamic), that determines if the element retrieves data or not. This works fine, EXCEPT on initial page load - where the property is completely ignored, since there is no way to get the value I know of. I currently retrieve that property during the update routine, which only occurs after page load. The way I see it, due to the lack of this capability - I have 4 options and they are all terrible:

  1. Change the default (currently false) to true - to allow data retrieval on first load.
  2. Create a separate element to handle the true vs. false use case.
  3. Use a shared key to determine the global behaviour of the plugin.
  4. Create some REALLY hacky solution that somehow causes the update routine to run immediately after the initialisation, to force the properties to be retrieved.

As above, I agree these are all terrible options - but without this most basic functionality, I am really lost in terms of how to achieve this…

Hmm…doesn’t an update run after the initialization? That’s been my experience

Have just tested this, and you are indeed correct, and it helped me narrow down the cause a bit further. I’ve been fighting an issue for days, where no matter what I did I could not get the properties to read. Just goes to show - sometimes it takes going back to basics!

Turns out it was a syntax error that was apparently silently causing a block of code to not run. Nothing worse than a silent error…

1 Like

Oh man. Syntax errors giving me PTSD.

Initialization runs one time when the element first becomes visible. Update runs whenever properties changes.

Tip:
initialize: instance.data.i = 0

update: if(instance.data.i === 0){ instance.data.i++ //run code here }

This will save you a headache