Step 2 - Create A Message Handler
Message Handlers are effectively user-created scripting events. A user can define a Message Handler that listens for a particular message. The idea being that some other component will broadcast a message, and if the type of the message matches what the Message Handler is listening for, the Message Handler will execute a script.
Message Handlers are useful because they don't care about which component broadcast the message: if something sends a message with the correct type, then the Message Handler will execute. Additionally, message can be sent across separate views, pages, or throughout the entire session.
In our example, we want the Label's text property to change when something else in the view happens (in our case, our button is pressed), so it would make sense to configure a Message Handler (listener) on the Label. This way, if we relocate the Label component in the view, the script will still work.
my-handler
. Note that we're using all lowercase characters: message handlers are case-sensitive. The Message Type is effectively the name of the message handler. When sending a message, we will specify this message handler's type, which will cause it to respond by executing the script. In our example, let's change the text on the Label to "Hello!". Under the script area, type the following script:
self.props.text = 'Hello!' |
In this step, we place a script on the Button that will call our Message Handler.
Add the following code:
messageType = 'my-handler' system.perspective.sendMessage(messageType) |
The window should look like the following. Note the tab indentation on lines 4 and 5:
Click OK to apply the script.
The example is now running. From the Designer, enable Preview mode, and then click on the Button component. The text on the label should update.
In this simple example, the major issue you may run into is the Message Type. Recall from #4 in Step 1 of the example, that Message Type is case-sensitive, so make sure the script on the Button is correctly referencing the message type, and try again. |
Let's make the previous example more complicated and pass some values with the message. The example above can be modified to determine the timestamp. When passing parameters in a script, the most direct approach is to include any parameters along with the message. Message Handlers have a built-in argument called a payload, which is used to transfer values to the handler. The payload is simply a Python Dictionary, so please see the Dictionaries page for more information.
Alternatively, we could create a session property to hold the value, and have the label reference the session property. However this would require that we either create a new property to hold the value or overwrite the value of another property. Thus, our next example will demonstrate how to utilize the payload.
Since Tag values are shared by all Perspective sessions, you may not want to write the parameters in Tags. Doing so would result in each session instance potentially trying to overwrite the same value. |
Replace the code with the following:
messageType = 'my-handler' # Look up the current time. currentTime = system.date.format(system.date.now(), 'HH:mm:ss') # Create a payload to be passed with the message payload = {'time':currentTime} # Send the message, pass the payload, limit the scope to the view system.perspective.sendMessage(messageType, payload, scope = 'view' ) |
Make sure the code is properly indented. It should look like the following:
On line 8, we're creating a dictionary, creating a key called "time", and storing the current time with the "time" key. When our handler receives the payload, it can retrieve the value we passed by referencing the "time" key. |
Now that we're including a payload with the message, we need to modify our handler so that it will extract the time from the payload.
Replace the original code with the following:
# Access the time by referencing the 'time' key self.props.text = payload['time'] |