The stateVar or SV object provides methods for controlling all state variables. SV is an alias for the stateVar object. Both SV and stateVar are properties of the window object and do not have to be imported.
SV.addHistory()SV.addHistory(id, value)
Adds a state variable ID and value to the global state variable history. The StateVar superclass automatically does this when a changed value is assigned to a state variable marked for saving (usually a user input). SV types that override the assignment method without calling the superclass will need to call this themselves.
idvalueSV.all()SV.all()
Returns an array containing all public state variables.
An array of state variables.
SV.allIds()SV.allIds()
Returns an array containing the IDs of all public state variables.
An array of state variable ID strings.
SV.create()SV.create(info)
Creates a new public state variable. The ID in info.id is published in the global sv namespace, and is searchable by the find() method. The SV type in info.type can be a string defined by the defineType() method or a class.
Non-public state variables can be created using the state variable class constructor.
info
An object containing the properties required for the SV type. info must contain:
idtype info may also contain these properties from the StateVar superclass, in addition to properties unique to the subclass:
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''local'onError
A function to be called when an invalid value is assigned.
The function signature is fn(sVar, newValue):
sVarnewValueA new state variable.
SV.defineType()SV.defineType(typeName, typeClass)
Associates the String typeName with the class typeClass. The string can be used to as the SV type to create a new state variable.
typeNametypeClassSV.find()SV.find(...)
Find all public state variables meeting the criteria expressed in the arguments and return them in an array. If more than one argument is present, then state variables that meet any of the criteria are returned.
Each parameter specifies a single matching criteria. The meaning of each parameter depends on its type:
For example,
SV.find(sVar => (sVar.page === myPage && sVar.isInput))
will return an array of all Model Input state variables associated with myPage.
SV.getHistory()SV.getHistory(count)
Return an array of the last count entries of state variable history ordered from oldest to newest. Each entry is an array with the state variable ID as the first element and the value as the second. This method is useful for debugging and is also used by the Controller to save some amount of history with the saved state.
countAn array of history elements, each with the state variable ID as the first element and the value as the second.
SV.getSavedState()SV.getSavedState(sVars)
Return an object containing the saved state of the state variables in the array passed as an argument. For each state variable, the returned object has a property that is the state variable ID with a value that represents the saved state variable state. The value may be a primitive type or an object, depending on the SV type.
sVarsAn object with a property corresponding to the ID of each state variable in sVars. The property value is the state variable value.
SV.getSv()SV.getSv(idOrSv)
Get a StateVar object. If idOrSv is already a StateVar object, it is returned. If idOrSv is a valid state variable ID then the associated StateVar object is returned. Otherwise, getSv() produces an assertion failure.
idOrSvA StateVar object.
SV.getTypeClass()SV.getTypeClass(typeName)
Return the state variable class associated with typeName.
typeNameThe StateVar class or a subclass of StateVar.
SV.isStateVar()SV.isStateVar(arg)
Returns true if arg is a state variable (an instance of StateVar) or an ID string for an existing state variable.
argA Boolean. True if arg corresponds to a state variable.
SV.isSvError()SV.isSvError(arg)
Returns true if arg is an instance of StateVarError.
argA Boolean. True if arg is an instance of StateVarError.
SV.isValid()SV.isValid(...)
Return true if all arguments are valid. Arguments that are undefined, NaN, or instances of StateVarError objects are invalid. If an argument is a state variable ID string or a state variable object, it's valid if the associated state variable is valid.
Each parameter is a value to be tested for validity.
A Boolean. True if all arguments are valid values or are state variables that have valid values.
SV.namespace()SV.namespace(...)
Return an object containing a property that is the ID for each state variable meeting the criteria expressed in the arguments (same as find()). The associated value is the state variable object. If more than one argument is present, then state variables that meet any of the criteria are included. The returned object is frozen and cannot be modified.
Each parameter specifies a single matching criteria. The meaning of each parameter depends on its type:
fn(sVar). All state variables for which calling the function with the state variable as the sVar argument returns true are added to the returned object.A frozen object containing properties for all state variables meeting any of the criteria. The property name is the state variable ID, and the property value is the state variable object.
SV.parseId()SV.parseId(id)
Returns an object with the following properties:
pageid.nameid.subNamesThe ID must be of the form '<page>_<name>', '<page>_<name>_<subName>', or '<page>_<name>_<subName>_<subName>...'
A state variable ID string.
An object.
SV.setSavedState()SV.setSavedState(state)
Restore the state of state variables from the state object. The state object is the same as the one returned by getSavedState(). For each enumerable property in state, the the key is used to find the associated state variable object, and then the state variable's setSavedState() method is called with the property value. The value may be a primitive type or an object depending on the SV type.
stategetSavedState() method.SV.toIds()SV.toIds(SVars)
Returns an array of IDs corresponding to the state variables in the sVars array.
sVarsReturns an array of state variable ID strings.
SV.toPageIds()SV.toPageIds(SVars)
Returns a deduplicated array of the page properties from the state variables in the sVars array.
sVarsReturns an array of page ID strings.