Variables
Variables allow developers to store and reference data, and communicate between different parts of an application. With variables, developers can create user interfaces with dynamic features and interactions, and build complex business logic.
Variable scope
Page-level variables
Page-level variables are valid only within the specific page where they are created. They are mainly used to manage page state, such as form inputs and control states. Page-level variables are not suitable for data sharing across pages.
Global variables
Global variables are valid across all pages of an application. They are suitable for application-wide data sharing, such as user configurations and authentication status. Note that global variables cannot reference any page-specific components or properties.
Variable references
Variables can use expressions to reference other variables or component properties in the application. This reference method allows variables to respond dynamically to data changes.
Example
To reference the value of an input component named input1, use the following syntax in the variable expression: {{ input1.value }}.
When the value of input1 changes, any variable that references it is also updated.
Variable types
Mobi supports the following six variable types for different data processing needs:
string: The string type.number: The number type.boolean: The Boolean type.object: The object type.array: The array type.any: The any type. No forced type conversion is performed.
When you set a variable type, the platform attempts to convert the variable's value to the configured type, except for the any type.
Variable descriptions
You can configure a description for a variable to help developers understand its purpose and context. The description is for developer reference and is not used by the platform's logic.
Create and use variables
Open the Code panel, select the target scope, click +, and then select Variable.
Enter an initial value for the variable or use an expression to reference another variable or component property. Select the variable type and add a description if needed.
Use the defined variable as needed.
Notes
Understand and correctly use variable scopes. Ensure that global variables do not reference page-specific elements.
Select the appropriate variable type to ensure data consistency and correct logical processing.
Use descriptions to improve code readability and maintainability.
FAQ
Q: How do I share data between different pages?
A: Use global variables to share data across all pages of the application.
Q: What happens to a variable if the component it references is deleted?
A: If a component or property that a variable references is deleted, the expression for the variable's value may result in an error. You must check and update the relevant variable references.
Q: Does variable type conversion affect data?
A: If a variable's actual data type does not match its configured type, the platform attempts a type conversion. In some cases, this conversion can lead to data loss or incorrect behavior. Therefore, selecting the correct variable type is essential to ensure data accuracy.