In most Perspective scripts, you are given a reference to a component object (often in the form of a self parameter). The object is given in component scripts, but has several methods and properties associated with it to help traverse to the other objects in a Perspective View or get values from the Session.
Object Traversal is limited to a single view. If a script needs to reference a component in a different view, or there is a possibility that the hierarchy of the view will change, then Message Handling should be utilized instead.
|
If you want to get other component properties in a view while scripting, you can use the above methods and properties to move around the View.
These examples assume you have the following structure/components in a View:
# this example code exists on the 'Button 1' in the above hierarchy. # get to the view that the button is in view = self.view # get the text from 'Text Field 1' text1 = self.getSibling('Text Field 1').props.text # get the text from 'Text Field 2' text2 = self.getSibling('Container 2').getChild('Text Field 2').props.text # get the text from 'Text Field 3' text3 = self.getSibling('Container 3').getChild('Text Field 3').props.text # get the text from 'Text Field 4'. Either of these will work the same. text4 = self.getSibling('Container 3').getChild('Container 4/Text Field 4').props.text text4 = self.getSibling('Container 3').getChild('Container 4').getChild('Text Field 4').props.text |
# this example code exists on the 'Button 4' in the above hierarchy. # get to the view that the button is in view = self.view # get the text from 'Text Field 1' text1 = self.parent.parent.getSibling('Text Field 1').props.text # get the text from 'Text Field 2' text2 = self.parent.parent.getSibling('Container 2').getChild('Text Field 2').props.text # get the text from 'Text Field 3' text3 = self.parent.getSibling('Text Field 3').props.text # get the text from 'Text Field 4' text4 = self.getSibling('Text Field 4').props.text |
Perspective components contain several shared methods. This section details such methods. Note that some methods are only available to certain types of components. In these cases, the description for the method will state any limitations.
The refreshBinding
function can be used to manually fire a binding, and is designed to be used on bindings that can poll (like query and Tag history bindings). In these instances, using refreshBinding in lieu of polling can save Gateway resources. The refreshBinding() function takes a string as a parameter, corresponding to the property that should be refreshed:
self.refreshBinding("props.data") |
It is often useful to use refreshBinding() from a component message handler, since we can then refresh several applicable bindings via a single system.perspective.sendMessage call.
The focus
method can be called by a component to request focus in a view. This is useful if you wish to control where keyboard input is directed after a particular action.
Due to the nature of focus, calling the focus
method is only effective on components that can have focus. Input components such as the Text Field and Numeric Entry Field components can gain focus, but Display components like Labels and Images can not gain focus.
self.focus() |
Perspective offers the option of configuring your own methods for a component. To configure a custom method:
Add code to implement your method.
A self object is provided in every custom method, but should not be provided as a parameter when calling the function. |
To call this method, use:
self.myMethod(param1,param2) |
For example:
self.myMethod("Hi!","This is a test") |