Skip to main content
Version: 4.64

Event Parameters

Events in mos. are similar to functions or methods in other programming languages. You can pass data to an event as parameters, allowing you to reuse the same event with different values.

In addition, some actions can trigger events when they succeed or fail. In these cases, event parameters can provide access to information about the action's result.

Quick guide​

To use event parameters, you can use a fireEvent action and pass the parameters inside the eventParams field.

{
"type": "fireEvent",
"params": {
"eventType": "checkId",
"eventParams": {
"id": "{{id}}"
}
}
}

In this example, the event parameter id is passed to the checkId event.

Using event parameters​

Simple passing of parameters when firing an event​

Use a fireEvent action to trigger an event.

The minimal configuration requires the eventType field, which specifies the name of the event:

{
"type": "fireEvent",
"params": {
"eventType": "showInformationPopUp"
}
}

To pass parameters to the event, add the eventParams field. It contains the parameter names and their corresponding values:

{
"type": "fireEvent",
"params": {
"eventType": "showInformationPopUp",
"eventParams": {
"title": "$lang(informationTitle)$",
"description": "$lang(informationDescription)$"
}
}
}

Using queries when firing an event​

You can also pass data from a database query to an event. Configure the fireEvent action with the table, query, and template to use:

{
"type": "fireEvent",
"params": {
"eventType": "getData",
"table": "products",
"query": "getProductData",
"queryMode": "single",
"queryParams": [
"{{id}}"
],
"template": "productData"
}
}
caution

When queryMode is set to all, the event is fired once for each row in the query result.

Using event parameters inside the event​

Event parameters can be accessed like other data values within a template. By default, you can use the delimiters {{ and }} to reference them.

For global events defined in the ./event directory, the default delimiters can be used directly.

For events defined inside views, you need to configure custom delimiters. This is because the default delimiters are replaced when the view is created. Use the leadingDelimiter and trailingDelimiter fields to define custom delimiters.

The following example uses {< and >} as custom delimiters to access the id event parameter:

{
"type": "showId",
"leadingDelimiter": "{<",
"trailingDelimiter": ">}",
"actions": [
{
"type": "showMessage",
"params": {
"text": "The ID was {<id>}"
}
}
]
}

Interaction between event parameters and action results​

Event parameters work like a dictionary containing keys and their corresponding values. Actions that support onSuccess and onError parameters can add or modify entries in this dictionary.

This happens when the corresponding type (onSuccessType or onErrorType) is set to return. The value specified in onSuccess or onError is then used as the key for the action result.

Adding an action result as a parameter​

Event parameters before the action:

{
"oldValue": "I am your king."
}

Action:

{
"type": "request",
"params": {
...,
"onSuccessType": "return",
"onSuccess": "requestResult"
}
}

Event parameters after the action:

The action result is added under the key requestResult. Existing parameters remain unchanged.

{
"oldValue": "I am your king.",
"requestResult": {
"statusCode": 200,
"headers": {},
"response": {
"answer": "Well, I didn't vote for you."
}
}
}

Replacing an existing parameter with an action result​

Event parameters before the action:

{
"oldValue": "I am your king.",
"secondValue": "Be quiet!"
}

Action:

{
"type": "request",
"params": {
...,
"onSuccessType": "return",
"onSuccess": "secondValue"
}
}

Event parameters after the action:

Because the key secondValue already exists, its previous value is replaced by the action result.

{
"oldValue": "I am your king.",
"secondValue": {
"statusCode": 200,
"headers": {},
"response": {
"addition": "I didn't vote for you."
}
}
}