Events execute a sequence of actions when triggered. They can be triggered explicitly using the fireEvent action or automatically by actions that support onSuccess or onError events, such as the request action.
Events can be defined locally within a client template or globally for the entire application.
- Local events are available within the client template in which they are defined.
- Global events are available throughout the application.
Quick guide
Local events are defined in the events array of a client template. Global events are defined as individual JSON files in the ./event directory.
An event consists of a type and an array of actions to execute when the event is triggered.
{
"type": "myEvent",
"actions": [
{
"type": "showMessage",
"params": {
"text": "My event has been triggered."
}
}
]
}
Event file structure
Every event consists of a type field and an actions array.
The type field identifies the event when it is triggered. The event type can be chosen freely, except for the names reserved for predefined events.
The actions array contains the actions to execute when the event is triggered.
Local events
Local events are defined in the events array of a client template.
{
"name": "myTemplate",
"content": {
"events": [
{
"type": "myEvent",
"actions": [
{
"type": "showMessage",
"params": {
"text": "My event has been triggered."
}
}
]
}
]
}
}
This example defines an event named myEvent that executes the showMessage action when triggered.
For example, such an event could notify the user after a successful request to a third-party API using the request action.
To define events in a template, the template must be a node template. Set templateIsNode to true. See showView.
Global events
Global events are defined as individual JSON files in the ./event directory.
Each event file contains a name field and a content object.
The name field should match the filename and identifies the event file.
The content object contains the type field and the actions array.
{
"name": "myEvent",
"content": {
"type": "myEvent",
"actions": [
{
"type": "showMessage",
"params": {
"text": "My event has been triggered."
}
}
]
}
}
Predefined events
Predefined events are triggered automatically when specific application or view lifecycle events occur, or when certain application states change.
Global lifecycle
The following events are triggered by changes in the application lifecycle:
appDidStart (planned name: onAppDidStart): Triggered after a fresh app start, for example, after the app has been completely removed from memory.
appResumed (planned name: onAppResumed): Triggered when the app enters the foreground, for example, when the user returns from another app.
appPaused (planned name: onAppPaused): Triggered when the app enters the background, for example, when the user switches to another app.
Other predefined global events include:
newPushToken (planned name: onNewPushToken): Triggered when the app receives a new push notification token.
audioPlaybackFinished (planned name: onAudioPlaybackFinished): Triggered when an audio file started with playAudio finishes playing.
darkModeEnabledChanged (planned name: onDarkModeEnabledChanged): Triggered when the app's dark mode setting changes.
updateAvailable (planned name: onUpdateAvailable): Triggered when a configuration update becomes available.
onlineStatusChanged (planned name: onOnlineStatusChanged): Triggered when the online status changes. The isOnline parameter contains the new status.
networkTypeChanged (planned name: onNetworkTypeChanged): Triggered when the network type changes. The networkType parameter contains the new type: wifi, ethernet, cellular, or unknown.
Local lifecycle
The following predefined events are triggered by changes in the lifecycle of a specific view:
viewCreated (planned name: onViewCreated): Triggered when a view is created.
viewResumed (planned name: onViewResumed): Triggered when a view returns to the foreground, including after a system dialog on Android.
viewPaused (planned name: onViewPaused): Triggered when a view moves to the background, including when a system dialog appears on Android.
viewDestroyed (planned name: onViewDestroyed): Triggered when a view is destroyed, for example, when navigating away from a screen.
Delimiters
Data passed to an event can be accessed using Mustache-like syntax, such as {{data}}.
In some contexts, these delimiters are reserved for other purposes, such as data binding in a client template. In these cases, custom delimiters can be specified using the leadingDelimiter and trailingDelimiter parameters of the event.
Conditional events
Events can have conditions that determine whether their actions are executed. An event can also define an optional elseActions array, which is executed when the condition is not met.
{
"type": "showViewCount",
"leadingDelimiter": "{$",
"trailingDelimiter": "$}",
"conditions": {
"left": {
"context": "parameter",
"field": "viewCount"
},
"mode": "greaterThan",
"right": "0"
},
"actions": [
{
"type": "showMessage",
"params": {
"text": "There have been {$viewCount$} views so far."
}
}
],
"elseActions": [
{
"type": "showMessage",
"params": {
"text": "There have been no views so far."
}
}
]
}
This example checks whether the viewCount parameter is greater than zero.
- If the condition is met, the event displays a message containing the current view count.
- If the condition is not met, the event executes
elseActions and displays an alternative message.
The example uses custom delimiters to access viewCount without conflicting with the default Mustache delimiters.
The elseActions array is optional. The same behavior can also be implemented using an if action or multiple conditions.
Troubleshooting
If an event does not work as expected, check the following:
- Is the event defined in the correct location: the client template for a local event or the
./event directory for a global event?
- Does the event's
type match the identifier used when triggering it?
- Are the actions defined correctly in the
actions array?
- If the event uses conditions, are the conditions and any
elseActions configured correctly?
- If the event uses parameters, are the correct delimiters configured?