This lesson is part of the Scripting in Ignition course. You can browse the rest of the lessons below.

LESSON LIST

Autoplay Off
Take topic challenge

Description

Learn how to use Component Message Handlers to pass data between components and views.

Video recorded using: Ignition 8.0

Transcript

(open in window)

[00:00] In Perspective, a message handler is a script that is typically placed on a component, and it can be called from just about anywhere from within the session, using the system.perspective.sendMessage function. To make a message handler, you need to find a component within your view. Right click on that component and go down to the Configure Scripts option. Once here, in the upper left hand corner, you can double click on the Add Handler option to add a new message handler. You then need to give your message handler a name. The name of the message handler is what the system function uses to invoke the message handler. You can also specify listen scopes. Listen scopes are where the message handler should listen for a call to execute from the send message function. This allows the send message function to call message handlers on other views, pages, or anywhere within the session. Finally, down below, you implement your message handler using the Python scripting language. There are two arguments available to you: the self argument, which is a reference to the component that the message handler is located on; and the second is the payload object, which is simply a collection of data that is passed in from the send message function. Now once you've created your message handler, like I mentioned earlier, you invoke the message handler by using the system function system.perspective.sendMessage. You simply need to pass the send message function the name of the message handler that you want to invoke. All message handlers that are listening in the scope that the send message function is run in will then execute. Let's take a look at an example of the message handlers in action. I have here on my buttons onClick event a script action that is running system.perspective.sendMessage, and it's looking for a message handler called refresh. Also on this view are two nearly identical tables. If I take a look at the bottom table, we can see that it has a binding on the data property. It's a query binding, looking at a table called Equipment in my database. I can also see that the query is set to poll at a one second rate, meaning the table will automatically update with new data as it's entered into the database. My other table has a similar binding on the data property, looking at the Equipment table in my database. However, the difference is that this binding is not set to poll, meaning it won't update when new data is inserted into the database. Lastly, this table has a message handler on it. If I take a look at it, it's a message handler called refresh. It simply calls the refresh binding function on the table component. The refresh binding function is a special function available to every component that simply refreshes the binding that you specify. In this case I am calling the refresh binding function on the self object, which is a reference to the table, and I am asking it to refresh the binding on the props.data property. This will tell the query binding to rerun its query and pull in new data. I already have a Perspective session open where we can try this out. Again, I have my two tables and the button that's going to refresh the upper table. I'm now going to insert some new data into my database, and you'll notice that when I do, the lower table immediately updates to show that new data, because its polling is turned on. However, no matter how long we wait, because the upper table is not polling, it's never going to pull in that new data from the database. I can click on my button, which is going to call that refresh message handler on the table, pulling in the new data from the database. Again, the real power of these message handlers is that they can be invoked from anywhere within the session. In theory, I can have a refresh message handler on every component, and so no matter where I am within the session, clicking a button that invokes the refresh message handler will call all instances of that message handler and cause every binding to refresh and pull in new data.

You are editing this transcript.

Make any corrections to improve this transcript. We'll review any changes before posting them.