Managing Events
Overview
Bellini has no dedicated event type. Instead, events are a pattern built on exported Function declarations: when something meaningful happens inside a component — a button is clicked, an item is selected, a form is submitted — the component calls an exported function, and the consumer wires up a handler to respond.
This pattern mirrors how built-in components work. Properties like On Click and On Change on built-in components are exactly the same mechanism.
What You Will Learn
- How to define a component event using an exported function declaration
- How to fire the event from inside the component
- How to handle the event as a consumer
- A complete worked example from both perspectives
When To Use This
Use this pattern when a component needs to notify its consumer that something has happened — for example, a button was clicked, an item was selected, or a form was submitted — and the consumer needs to respond with its own logic.
Defining an Event
To define an event, create a Function declaration in the Component Editor and mark it as exported.
By convention, event declarations are named with an on prefix that describes what happened:
onSendClickonItemSelectedonFormSubmit
If the event carries data that the consumer needs to act on, declare those as Parameters in the export configuration. The parameter names are shown to the consumer in the expression editor when they wire up their handler, so use descriptive names.
An event function typically does not have a return value — its purpose is to notify, not to compute. Because the function body is provided by the consumer, an exported function cannot have its own implementation inside the component. Do not try to give it one.
For the export configuration UI, see Declarations — Exported Declarations.
Firing an Event
To fire an event, call the exported function from inside the component — in a function body or expression
— using $ctrl:
1 | |
If the event carries data, pass it as arguments:
1 | |
Exported functions are always safe to call. They are never null or undefined, so no guard is needed.
Handling an Event
From the consumer's perspective, an event appears in the Properties view under the component's section, named after the exported function (e.g. "On Send Click"). The consumer wires up a function declaration from the current page or component as the handler.
By convention, handler functions are named with a handle prefix that mirrors the event name:
handleSendClickforonSendClickhandleItemSelectedforonItemSelected
For the full UI details on wiring up function bindings, see Exported Properties and Functions — Setting Function Bindings.
Worked Example
The following example shows an OrderSummary component that fires an event when the user clicks a
send button, passing the current order ID back to the page.
Component author: define and fire the event
- In the Declarations panel, add a Function declaration named
onSendClick. - Check Export and add a parameter named
orderId. - Add a Function declaration named
handleButtonClick(not exported) with this body:
1 | |
- Bind the internal button's On Click property to
$ctrl.handleButtonClick().
Consumer: handle the event on the page
- On the page, inject an API service (e.g.
OrdersApi) via the Declarations panel. - Add a Function declaration named
handleSendClickwith a parameterorderId, and call the API service in the function body:
1 2 3 | |
Note: To use
awaitin a function body, check the Async checkbox in the function editor dialog.
- Select the
OrderSummarycomponent on the page and open the Properties view. - Locate On Send Click and click the edit icon to open the function picker.
- Select
handleSendClick— Bellini generates the expression:
1 | |
The orderId parameter defined on the event is automatically available in the expression editor's
Parameters tab, ready to be passed through to the handler.
Best Practices
Prefix event declarations with on. This communicates intent immediately and is consistent with
built-in component conventions. A consumer scanning the Properties view understands that onSendClick
is something to react to, not a value to configure.
Prefix handler functions with handle. handleSendClick is clearly a response to a specific event,
not a general utility function. It also makes the pairing between event and handler obvious at a glance.
Keep parameters descriptive and minimal. Only pass what the consumer actually needs to handle the
event. A parameter named orderId tells the consumer exactly what to expect; a generic data or args
does not.
Fire events from functions, not directly from UI bindings. Wiring a built-in button's On Click
directly to $ctrl.onSendClick(orderId) works, but wrapping it in an internal function keeps the
component's logic in one place and makes it easier to extend later (e.g. validate before firing).
Helpful Resources
- Exported Properties and Functions — how exported declarations appear and are configured in the Properties view
- Declarations — Exported Declarations — export configuration reference
- Custom Components overview — component types, scope, and the component editor
- Community Q&A: Bellini Community
Have a question? Post or search it here.