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.
Object Traversal Examples
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:
- Button 1
- Text Field 1
- Container 2
- Text Field 2
- Container 3
- Text Field 3
- Container 4
- Button 4
- Text Field 4
# 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. Some methods are only available to certain types of components. In these cases, the description for the method will state any limitations.
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:
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.
Perspective offers the option of configuring your own methods for a component. To configure a custom method:
- Right-click on the desired component in the Designer, and select Configure Scripts...
- Under Custom Methods, double click on Add method...
- Enter a Name for your method, which will be used to call the method.
- Enter any number of Parameters your method will need, separated by commas.
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.
- Click OK to commit your method.
To call this method, use:
self.myMethod("Hi!","This is a test")