## Implementing guiEvent

The guiEvent function serves as a gateway for all events. Therefore, a typical implementation will consist of switching logic that dispatches the events to other functions that actually provide functionality to handle the events. In this section the calling syntax, arguments and return values will be discussed, followed by a small example.

### Syntax

function payload = guiEvent(metaData, payload)
...
end

def gui_event(meta_data: dict, payload: dict) -> dict:
...


#### Arguments

metaData:

Meta data describing the client session. It is a struct/dict with fields:

• session_id: unique ID for the session, can be used to differentiate between multiple instances of the application
• namespace: package name of the application

payload:

When entering the function, this contains the event data containing all values as provided by the client. It is a struct/dict with fields:

• action: 'event'
• download: information regarding the download of a file. Empty when no file is being downloaded.
• event: name of the event that was triggered.
• followUp (optional): name of a follow-up event that will be triggered after the current one has completed.
• formMap: Map representing the form definition.
• key: identifier of the component that triggered the event
• submission: struct/dict with fields:
• data: struct/dict with fields:
• eventCounter: incrementing counter; do not change.
• <key>: multiple fields containing the values of the components identified by the keys.
• <nested form key>: See Nested forms for more information. Struct/dict with fields:
• data: struct/dict with fields:
• <key>: multiple fields containing the values of the components identified by the keys.
• updateForm: when true, the form definition is sent to the front end. The default value is false. Use this only when the form definition changes. Updating the form definition will be slower than only updating the submission data.
• alerts (optional): array of struct/dict with fields:
• type: message type, see Alerts
• message: string

In the guiEvent function, the submission field may be altered before sending payload back to the front-end in order to present results to the user. Additionally, the followUp field can be changed, which is described below.

### Key & Event

The key is unique for each component within the context of its parent and/or nested form, but event can be shared between components. This can be useful when multiple components (partially) share functionality. See Example.

### Dispatching events

As the number of possible events in your application grows, your guiEvent function may contain an enormous switch/case or if/elif construction. In order to mitigate this, it is possible to automatically call functions based on event names without having to specify every single event. This can be achieved by using the dispatchEvent or getEventFunction utility functions:

payload = utils.dispatchEvent(metaData, payload);

caller = utils.getEventFunction(meta_data, payload)


The dispatchEvent function calls a function equal to the name of the event that was triggered, whereas getEventFunction only returns the function object.

Note: In Python, breakpoints cannot be detected in functions executed via the dispatchEvent function. It is therefore recommended to use getEventFunction instead.

The functionality of the dispatching mechanism is illustrated with the example below. Consider the initialization code of a form with several buttons, each with their own event:

btn1 = component.Button("btn1", parent);
btn1.setEvent("ModelS.getResults");

btn2 = component.Button("btn2", parent);
btn2.setEvent("writeToDatabase");

btn3 = component.Button("btn3", parent);
btn3.setEvent("ModelX.getValue");


The following guiEvent functions handle these events, the first one using conditional logic and the second one using the dispatchEvent or getEventFunction functions:

function payload = guiEvent(metaData, payload)
case "ModelS.getResults"

case "writeToDatabase"

case "ModelX.getValue"
end
end

% Use the automatic dispatch.
end

def gui_event(meta_data, payload):

# Use the automatic dispatch.


### Follow-up Event

When the followUp field is added to payload, and its value is the name of another event, the follow-up event will be triggered after completion of the current one. The component key will be identical for both events. The most common use case of this feature is to present the user with intermediate results of a calculation that consists of multiple stages. Consider combining this with the StatusIndicator component.

An example of the follow-up event is given below:

function payload = guiInit(metaData)
form            = Form();

txtId       = component.TextField("id", form);
txtId.label = "ID";
end

case "Run"
pause(1);

case "RunNext"
pause(1);

case "RunFinal"
pause(2);
end
end

import time

def gui_init(meta_data: dict) -> dict:
form = Form()

txtId = component.TextField("id", form)
txtId.label = "ID"

def gui_event(meta_data: dict, payload: dict) -> dict:
time.sleep(1)
time.sleep(1)
time.sleep(2)



### Example

The following example shows an implementation of guiEvent that uses the event names and component keys to dispatch the event to the correct function.

function payload = guiEvent(metaData, payload)
% Switch on event name.
case "calculate"
% The calculate event has an application-specific preprocessing step.
% Preprocess the data entered in the form before performing calculations.

% Switch on the component key.
% Each component starts a different calculation.
case "stage1StartButton"

case "stage2StartButton"
end

case "estimate"
end
end

def gui_event(meta_data, payload):
# Switch on event name.
# The calculate event has an application-specific preprocessing step.
# Preprocess the data entered in the form before performing calculations.