Skip to content

Script Examples

Emit Functionality

Emit Example:
Download ezUI File

Candidate Component:
Download ezUI File

In this example, we illustrate how to use the emit functionality to handle user interactions within a Candidate Component. Users can input data such as first and last names and select a headshot. The system listens for changes in these fields and dynamically updates the component's data, emitting a 'candidateChange' event with the new information.

EmitFunctionality

  1. The first step is to create a 'Candidate' component, in order to do this:

    • press the UI Manager button
    • go to the Components section
    • press + and type in the component name
    • press Create and then Open.

    CreateComponent

  2. Import the Candidate Component.ezui file, then save the created component.

  3. Duplicate the Studio window, open some template.
  4. Import the Emit Example.ezui file.
  5. In the Components section press Add component button.
  6. Choose the component you have just created, press Update.
  7. In the Components list find your component (it is in the bottom of the list), drag&drop it to the appropriate section, and delete the excessive one component instead of it.
  8. Instead of nameControl in the script, please type the Name of your Component and press Save.

CandidateComponentChangeName

This ensures that the component reflects the latest user input in real-time. The design ensures smooth user interactions, with updates happening instantly as data changes are detected.

How emit reaches the hosting UI

emit(name, args) fires the handler on<Name> (the first letter of the event name is upper-cased) on the root of the component, and the event then bubbles up through the parent chain. That is why the UI that hosts the component can listen on the component instance itself.

Both listener styles work on the host side:

1
2
3
4
5
// Assignment form - replaces any previously assigned handler
nameControl.onCandidateChange = e => { /* ... */ };

// Listener form - adds a handler, existing handlers are kept
nameControl.on('candidateChange', e => { /* ... */ });

The event object carries the properties that were passed to emit (here name and headshot), plus e.sender (the control that emitted the event), e.event (the handler name, onCandidateChange) and e.stopPropagation(), which stops the event from bubbling any further up the parent chain.

Tip

A component also exposes its own child controls to the hosting UI, so nameControl.firstName.value reads the component's firstName text input directly. Only functions and variables have to be declared with export.

Emit Example:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
// Build the summary text from the data the component sent
function createSummary(candidate: { name: string, headshot: string }) {
  summary.value =
    `Name: ${candidate.name}\n` +
    `Headshot: ${candidate.headshot}`;
}

// Read the current data once, using the function the component exports
createSummary(nameControl.getData());

// Update the summary every time the component emits 'candidateChange'
nameControl.on('candidateChange', e => createSummary(e));

Candidate Component:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
export function getData() {
  const name = `${firstName.value} ${lastName.value}`; // Combine first and last name
  const headshot = mediaBrowser.value; // Get headshot from media browser
  return { name, headshot }; // Return an object with name and headshot
}

// Function to handle changes and emit data
function onChange() {
  emit('candidateChange', getData()); // Emit 'candidateChange' event with data
}

// Register onChange function for input fields
firstName.onChange = onChange;
lastName.onChange = onChange;
mediaBrowser.onChange = onChange

Info

getData() is callable from the hosting UI because functions a component script exports are attached to the component element and resolved through the component instance.