Difference between revisions of "OnChange"
(4 intermediate revisions by 3 users not shown) | |||
Line 1: | Line 1: | ||
[[Category:Attributes]] | [[Category:Attributes]] | ||
− | + | ||
+ | __TOC__ | ||
+ | |||
The [[OnChange]] attribute of a variable contains an [[Expression Syntax|expression]] that gets evaluated when the [[Definition]] of a variable changes. | The [[OnChange]] attribute of a variable contains an [[Expression Syntax|expression]] that gets evaluated when the [[Definition]] of a variable changes. | ||
− | When a variable is defined as a [[Choice]] or [[Checkbox]], the expression is evaluated when the user changes the selection. For a [[Table]], the expression is not evaluated when individual cells are edited, but it is evaluated when the final edit is made (e.g., when the green checkbox is pressed or | + | When a variable is defined as a [[Choice]] or [[Checkbox]], the expression is evaluated when the user changes the selection. For a [[Table]], the expression is not evaluated when individual cells are edited, but it is evaluated when the final edit is made (e.g., when the green checkbox is pressed or Edit table window is closed). |
The expression can contain side-effects. For example, within the expression, you can assign to a global variable using the := operator. Such side-effects are not permitted in variable definitions. The result of the evaluation is not saved and is never used. Hence, the whole purpose of the expression is to enact some sort of side-effect. | The expression can contain side-effects. For example, within the expression, you can assign to a global variable using the := operator. Such side-effects are not permitted in variable definitions. The result of the evaluation is not saved and is never used. Hence, the whole purpose of the expression is to enact some sort of side-effect. | ||
The expression syntax is the same as the syntax for a [[Definition]] attribute. | The expression syntax is the same as the syntax for a [[Definition]] attribute. | ||
− | |||
− | |||
The [[OnChange]] expression is always evaluated in [[Evaluation Modes|mid-mode]]. | The [[OnChange]] expression is always evaluated in [[Evaluation Modes|mid-mode]]. | ||
− | Before you can access the [[OnChange]] attribute for a variable in the [[Attribute | + | Before you can access the [[OnChange]] attribute for a variable in the [[Attribute panel]] or [[Object Window]], you need to turn on the attribute visibility from the [[Manage_attributes#Attributes_dialog|Attributes Dialog]] on the '''Object''' menu. |
− | = Assignment = | + | == Assignment == |
+ | When you change the [[Definition]] of a variable using the [[Assignment_Operator::_::%3D|Assignment Operator, :=]], the [[OnChange]] expression for that variable evaluates, unless it is already running. The fact that it doesn't run a second time if already running enables some interesting capabilities. | ||
− | + | An [[OnChange]] attribute can change its own [[Definition]]. As an example, suppose that a variable <code>X</code> has the following as its [[OnChange]] expression: | |
+ | :<code>X := "" & (Definition of X)</code> | ||
− | |||
− | |||
When a user enters a value, it is always converted to text. When the value is first changed, [[OnChange]] runs. It in turn alters the value again, but [[OnChange]] is not launched a second time because it is already running. | When a user enters a value, it is always converted to text. When the value is first changed, [[OnChange]] runs. It in turn alters the value again, but [[OnChange]] is not launched a second time because it is already running. | ||
Two or more input variables can change each other via their [[OnChange]] attributes to keep them mutually consistent. For example, suppose you have two inputs variables, <code>GMT_Time</code> and <code>Local_Time</code>, and you have a variable named <code>Time_Offset</code> that relates the two. You set | Two or more input variables can change each other via their [[OnChange]] attributes to keep them mutually consistent. For example, suppose you have two inputs variables, <code>GMT_Time</code> and <code>Local_Time</code>, and you have a variable named <code>Time_Offset</code> that relates the two. You set | ||
− | + | :<code>OnChange GMT_Time : Local_Time := GMT_Time + Time_Offset</code> | |
− | + | :<code>OnChange Local_Time : GMT_Time := Local_Time - Time_Offset</code> | |
− | Now a user enters a time into the GMT_Time input. Local_time immediately adjusts. Or he enters a time into the Local_time input -- GMT_Time immediately adjusts. In the first case, <code>OnChange of GMT_Time</code> evaluates, which changes the definition of Local_time, which in turn triggers <code>OnChange of Local_Time</code>. Local_time actually changes the definition of GMT_Time again, but to the same value it already has, but <code>OnClick of GMT_Time</code> does not run again because it is already active; hence, the loop terminates. | + | |
+ | Now a user enters a time into the <code>GMT_Time input</code>. <code>Local_time</code> immediately adjusts. Or he enters a time into the <code>Local_time</code> input -- GMT_Time immediately adjusts. In the first case, <code>OnChange of GMT_Time</code> evaluates, which changes the definition of <code>Local_time</code>, which in turn triggers <code>OnChange of Local_Time</code>. Local_time actually changes the definition of <code>GMT_Time</code> again, but to the same value it already has, but <code>OnClick of GMT_Time</code> does not run again because it is already active; hence, the loop terminates. | ||
+ | |||
+ | == Inputs and Outputs == | ||
+ | If the [[OnChange]] attribute of <code>X</code> uses the value of <code>Y</code>, then <code>Y</code> will show up on the list of '''Inputs''' for <code>X</code> in the [[Object Window]], and <code>X</code> will be in the list of '''Outputs''' for <code>Y</code>. If <code>Z</code> is assigned to in the [[OnChange]] attribute of <code>X</code>, then <code>Z</code> will appear as an '''Output''' of <code>X</code> and <code>X</code> will appear as an '''Input''' of <code>Z</code>. However, on the diagram, arrows are not show to depict these dependencies. | ||
− | = | + | == History == |
+ | The [[OnChange]] attribute was introduced in [[Analytica 4.6]]. It replaces the [[Script]] attribute when used for [[Choice]] and [[Checkbox]] controls prior to [[Analytica 4.6]]. You can use [[OnChange]] for any variable, including text inputs, where [[Script]] was only available for user input variables using [[Checkbox]] or [[Choice]] functions. [[OnChange]] expects an [[Expression_Syntax|expression]] using the standard Analytica syntax, where the [[Script]] attribute expects [[Typescript]], which meant you had to learn a slightly different syntax to use it. The [[Script]] attribute still works in 4.6 so that legacy models still work. If a variable contains both [[OnChange]] and [[Script]], it evaluates [[OnChange]] before [[Script]]. | ||
+ | == See Also == | ||
+ | * [[Onchange attribute]] | ||
* [[Choice]] | * [[Choice]] | ||
* [[Checkbox]] | * [[Checkbox]] | ||
* [[OnClick]] | * [[OnClick]] | ||
+ | * [[Creating Interfaces for End Users]] |
Revision as of 21:57, 15 January 2016
The OnChange attribute of a variable contains an expression that gets evaluated when the Definition of a variable changes.
When a variable is defined as a Choice or Checkbox, the expression is evaluated when the user changes the selection. For a Table, the expression is not evaluated when individual cells are edited, but it is evaluated when the final edit is made (e.g., when the green checkbox is pressed or Edit table window is closed).
The expression can contain side-effects. For example, within the expression, you can assign to a global variable using the := operator. Such side-effects are not permitted in variable definitions. The result of the evaluation is not saved and is never used. Hence, the whole purpose of the expression is to enact some sort of side-effect.
The expression syntax is the same as the syntax for a Definition attribute.
The OnChange expression is always evaluated in mid-mode.
Before you can access the OnChange attribute for a variable in the Attribute panel or Object Window, you need to turn on the attribute visibility from the Attributes Dialog on the Object menu.
Assignment
When you change the Definition of a variable using the Assignment Operator, :=, the OnChange expression for that variable evaluates, unless it is already running. The fact that it doesn't run a second time if already running enables some interesting capabilities.
An OnChange attribute can change its own Definition. As an example, suppose that a variable X
has the following as its OnChange expression:
X := "" & (Definition of X)
When a user enters a value, it is always converted to text. When the value is first changed, OnChange runs. It in turn alters the value again, but OnChange is not launched a second time because it is already running.
Two or more input variables can change each other via their OnChange attributes to keep them mutually consistent. For example, suppose you have two inputs variables, GMT_Time
and Local_Time
, and you have a variable named Time_Offset
that relates the two. You set
OnChange GMT_Time : Local_Time := GMT_Time + Time_Offset
OnChange Local_Time : GMT_Time := Local_Time - Time_Offset
Now a user enters a time into the GMT_Time input
. Local_time
immediately adjusts. Or he enters a time into the Local_time
input -- GMT_Time immediately adjusts. In the first case, OnChange of GMT_Time
evaluates, which changes the definition of Local_time
, which in turn triggers OnChange of Local_Time
. Local_time actually changes the definition of GMT_Time
again, but to the same value it already has, but OnClick of GMT_Time
does not run again because it is already active; hence, the loop terminates.
Inputs and Outputs
If the OnChange attribute of X
uses the value of Y
, then Y
will show up on the list of Inputs for X
in the Object Window, and X
will be in the list of Outputs for Y
. If Z
is assigned to in the OnChange attribute of X
, then Z
will appear as an Output of X
and X
will appear as an Input of Z
. However, on the diagram, arrows are not show to depict these dependencies.
History
The OnChange attribute was introduced in Analytica 4.6. It replaces the Script attribute when used for Choice and Checkbox controls prior to Analytica 4.6. You can use OnChange for any variable, including text inputs, where Script was only available for user input variables using Checkbox or Choice functions. OnChange expects an expression using the standard Analytica syntax, where the Script attribute expects Typescript, which meant you had to learn a slightly different syntax to use it. The Script attribute still works in 4.6 so that legacy models still work. If a variable contains both OnChange and Script, it evaluates OnChange before Script.
Enable comment auto-refresher