[SOLVED] How to pass a value from a button to a view on a perspective session

Good morning,

I have a coordinate view with a carousel component and an embedded component. My carousel component holds 5 pages of 24 buttons each. When the user presses a button i want to pass a value from the button to the embedded view. What is the best way to do this?

Each button has an actionPerformed script that does the following:

self.view.params.selectedUser = self.props.text

On the EmbeddedView i have a custom parameter called selectedUser but when I press a button it doesn’t seem to update this value. Any thoughts here are appreciated.

Use message handlers to pass data around between views. You can set the payload to be the info that you need to pass.

So basically have a message handler sitting on the embedded view that is looking for a message type of say ‘on-username-clicked’. On the button onClick script do something like this:

payload = {'userName':self.props.text}
system.perspective.sendMessage('on-username-clicked',payload)

Then on the embedded view have a message handler of type ‘on-username-clicked’ and do something like this:

userName = payload['userName']
self.view.params.selectedUser = userName
3 Likes

Is this the proper way to pass data between around? Or just one way out of many? My experience is very limited with Perspective.

For the most part yes. You can use bindings as well but typically not in the scenario that you have. Usually you bind going down in a 1 to 1 environment. If you have a 1 to many like you have then message handlers are you best friend.

1 Like

Ok, I got the gist of the solution down. I was able to pass the last button I clicked on to the embedded view.

Bindings work well within a View which will not have its structure changed in the future (changing component location within the View container hierarchy can break bindings), but as soon as you start interacting with Embedded Views, Popups, and Docked Views you will want to either bind to session properties - which can get messy if you need to to so for hundreds of properties - or you should use message handlers.

message handlers is amazing technique in Prespective.
Send your value to fly