StateVar is the superclass for all state variables. Its primary function is to store a value. The value can be any JavaScript type, but subclasses can restrict this. Subclasses can have other internal state including child state variables. This state is manipulated by other properties and methods and/or by directly using the child state variables.
The StateVar class does not schedule Model execution or View synchronization when the value is changed, though subclasses can modify this behavior.
The StateVar class is associated with the SV type string 'value'.
Note: Most state variables used by the Model and View, are subclasses of StateVar and are created during page creation by ctl.pageCreate(). See Model SV Type Reference and (Controller Reference)[controller.md].
new StateVar(info)
infoinfo object can contain the following properties. Other properties intended for subclasses are ignored by the StateVar constructor.id (required)
The state variable ID string.
value
The state variable’s initial value. The JS type of the value is dependent on the SV type.
save
The save property may take one of the following values:
'global''local''none'onError
A function to be called when an invalid value is assigned. The function signature is fn(sVar, newValue):
sVarnewValueaddChangeListener()addChangeListener(callbackFn)
Adds a listener function to be called whenever the value of the state variable is considered changed.
callbackFncallbackFn(sVar). The function is called when the state variable value is considered to be changed. It takes a single argument:sVarbreakOn()breakOn(type)
Causes a debugger breakpoint when the SV is assigned a value that meets the conditions associated with the type argument.
typetype may take one of the following values:'''change'isDifferent()).'get''invalid''set'createMonitor()createMonitor(noInitialChange)
Returns an SvMonitor for this state variable. If noInitialChange is truthy, the first invocation of the hasChanged() method on the SVMonitor will return false. Otherwise, it will return true. StateVar subclasses can override this method to return different subclasses of SvMonitor with different behavior.
noInitialChangehasChanged() method will return false. Otherwise it will return true.An SvMonitor for this state variable.
doChange()doChange()
Do what’s required when a change happens. By default, it calls all the change listeners. This may be overridden in a subclass, for example, to call the Controller to execute the Model.
forceChange()forceChange()
Force a change to be recorded. Runs the change listeners and forces subsequent hasChanged() invocations on SvMonitors to return true.
getSavedState()getSavedState()
Returns the state variable state. This may or may not represent the entire state of the state variable. For example, the state variable's className is usually excluded.
The return value type depends on the SV type. Simple state variables may return their value as a primitive type. More complex types might return an object representing their full internal state, including any subSvs.
isDifferent()isDifferent(value)
Return true if value is different from the current state variable value. This method is used internally to determine when a state variable value is considered changed. By default, isDifferent() returns true if value is different from the internal value according to Object.is().
valueA Boolean. Returns true if value is considered different from the current state variable value and false otherwise.
isValid()isValid()
Return true if the current state variable value is considered valid.
A Boolean. Returns true if the current state variable value is considered valid and false otherwise.
isValidState()isValidState(state)
Returns true if calling setSavedState(state) would result in a valid value. This give the caller a means to validate previsouly saved state.
stategetSavedState()A Boolean. Returns true if setting the state variable according to state would result in a valid value and false otherwise.
isValue()isValue(value)
Return true if value is considered the same as the current state variable value. This is only used externally and is not necessarily the inverse of isDifferent().
valueA Boolean. Returns true if value is the same as the current state variable value and false otherwise.
removeChangeListener()removeChangeListener(callbackFn)
Removes a change listener function previously added by the addChangeListener() method.
callbackFnaddChangeListener() method when the listener was set up.setSavedState()setSavedState(state)
Set the internal state of the state variable. This may or may not represent the entire state of the state variable. For example, the state variable's className is usually excluded.
stategetSavedState()val()val()
val(value)
With no arguments, return the state variable value. The allowed type of the value varies with SV type. With a value argument, sets the state variable value. If any breakOn() criteria are satisfied, a debugger break is executed. If an assigned value is considered changed, then any change listener callback functions are executed. These are the function forms of the value getter/setter.
value (optional)StateVarError instance.The state variable value. Only returned when no argument is provided. The JavaScript type is appropriate for the state variable.
These properties are available in every SV type:
idThe state variable’s ID string from info.id. An ID is required for all Model state variables. Other SV types may have an ID for debugging purposes.
A string of the form '<page>_<name>', '<page>_<name>_<subName>', or '<page>_<name>_<subName>_<subName>...'.
infoThe info object passed to the constructor.
An object.
isDisabledTrue if the state variable is an instance of an Input SV type and is disabled.
A Boolean or undefined.
isInputTrue if the state variable is an instance of one of the Input SV types.
A Boolean or undefined.
isNumberTrue if the state variable is an instance of the numericInput or numericOutput SV types.
A Boolean or undefined.
isParentTrue if the state variable is the parent of a childSv.
A Boolean or undefined.
isOutputTrue if the state variable is an instance of one of the Output SV types.
A Boolean or undefined.
isSelectorTrue if the state variable is an instance of one of the Selector Input SV types.
A Boolean or undefined.
isUnitCtlTrue if the state variable is an instance of the unitSelector SV type.
A Boolean or undefined.
isUnitParentTrue if the state variable is an instance of the unitInput or unitOutput SV types.
A Boolean or undefined.
parentIf the state variable is created by another state variable for storing internal state, this points to the creating state variable.
An state variable object or undefined.
saveThe value of info.save. The save property may take one of the following values:
'global''local''local'An state variable object or undefined.
childSvIf an state variable creates other state variables for its internal state, the childSv object holds pointers to the created state variables with the state variable sub-name as the key.
An object.
valueThe main value of the state variable. The type of the value varies with SV type. Subclasses of StateVar may have other internal state that is manipulated by other properties and methods and/or stored in child state variables.
A value appropriate for the SV type.