The problem
When building out screens that have interacting components, a common starting point from engineers that have used Vision in the past, is to use reference property paths or getChild
/getSibling
in scripting to reference other components.
Example
I.e. if you have a structure like this:
You might want to bind your label to your toggle switch, so you would do something like this:
or something that includes self.getSibling("ToggleSwitch").props.selected
. Inherently, this is what the designer does for you when you use the property browser!
The outcome and "solution"
However as soon as you start nesting components into other containers, this will all fall apart because your reference is now wrong. This is why I always guide engineers to move "view-level state properties" (I made that phrase up) to the view custom properties.
This would now give you a custom property on your view called isSelected
, then a bidirectional property binding on the props.selected
for the toggle switch, and a property binding on the props.text
for the label.
Now, whenever you move things around, all bindings still work because the view will never move, therefore the paths will always work.
This is my fundamental message in this post:
The real question...
This has been my default recommendation for years now, but the question is, what is wrong with the approach, and who has a different experience?
My Thoughts
My first and only thought as to why this solution might be "worse", is the performance implications of potentially doubling the necessary amount of bindings. However I don't know from an implementation standpoint how Perspective handles getting these properties. My guess is that using view custom properties causes the component to use a getView
method to return the view, and then read the custom prop. Versus the relative reference needs to first getView
and then getChild
multiple times until it hits the base component, and then read the prop found there. Ultimately ending up in more "hops" to the component property than just having two bindings pointing at the view in the first place.
Hopefully this thread can be educative to anyone else coming back afterwards, to streamline their path to Perspective development success!