↓ Skip to main content
A quilled paper rosette seen from above, one large coiled disc at the centre with spiralling ribbons radiating outward, each ending in a tight coil or an open teardrop

Events and Event Handlers

A user taps a button and something happens. Between those two facts sits one attribute in your markup and one method in your controller, and the wiring between them is simpler than most frameworks make it. This page shows the whole event surface, as the apps IBM ships use it, so you can see how small the part you actually need is.

MAS 9.2 series framework 3.2.8xx a handful of names

The patterns come from the apps that IBM ships with a 9.2-series install. The demonstration runs in our Components Explorer, a small Maximo Mobile app we built for these articles, in the preview of our MAF editor. Details move with each release, and the shape holds.

One attribute, one method
#

An event handler is an attribute whose value is the name of a method on a controller.

<button label="Save" on-click="saveRecord"/>
class DetailPageController {
  saveRecord() {
    this.page.findDatasource("woDS").save();
  }
}

There is no registration step and no import. The framework takes the string, looks for a method of that name on the controllers in scope, and calls it. If no controller has the method, nothing happens and nothing complains, which is the failure mode to remember.

Scope matters here, because an unhandled event travels from the datasource to the page to the application.

An event travels up until a controller has a method of that name An event travels up until a controller has a method of that name
An event travels up until a controller has a method of that name

A method defined on any of those three will catch it. A name defined in two of those places runs in both, which is a good reason to keep handler names distinctive.

We tried it with two buttons. The first names saveRecord, which our controller has: it runs and writes “saveRecord ran”. The second names saveRecrod, a typo. A tap on it changed nothing on the page, and the app wrote nothing to the browser console at its log level error.

<button id="fs_c_events_ok" kind="primary" icon="carbon:save"
        label="Save (saveRecord)" on-click="saveRecord"/>
<button id="fs_c_events_typo" kind="secondary" icon="carbon:save"
        label="Save (saveRecrod)" on-click="saveRecrod"/>

The framework stays silent, so the check has to come from your tools. Our MAF editor reads the controllers of the app and marks the typo at the element, before the app ever runs:

The MAF editor with the app FSCOMPM. On the left the structure with the button fs_c_events_typo selected. In the middle the preview shows the page with both Save buttons, the second one outlined, and the code view shows concepts-events.xml with a warning sign at line 12. On the right the properties of the button: on-click saveRecrod, with the note “saveRecrod is not in FsConceptsController” and a link “New method saveRecrod(event)”. At the bottom the problem list shows the warning “No controller of this app has a method ‘saveRecrod’ for on-click.” at concepts-events.xml:12.
Our MAF editor marks an on-click that names a method no controller has

Passing a value into the handler
#

Most handlers need to know which record was tapped. That comes from a second attribute with the same name plus -arg.

<button label="Open" on-click="openRecord" on-click-arg="{item}"/>
openRecord(item) {
  this.app.setCurrentPage({ name: "details", params: { href: item.href } });
}

The pairing is a rule the corpus keeps without exception. Many of the event attribute names end in -arg, and every one of them has a matching handler attribute of the same stem, with no orphans anywhere.

The eight names you will actually use
#

The distribution is steep. A handful of handler names carry the large majority of all handlers written across the shipped apps.

AttributeFires when
on-clickthe element is tapped
on-changean input’s value changes
on-primary-actionthe main button of a dialog or card is used
on-item-clicka row or card in a list is tapped
on-secondary-actionthe second button of a dialog is used
on-close-actiona dialog or panel is dismissed
on-actiona generic action element is triggered
on-bluran input loses focus

Listed from the most used to the least used, in the apps of the 9.2 series.

on-click alone is the most used handler by a wide margin, and it sits on more than one kind of element. Most of its uses are on buttons, followed by links and menu items, with a few on tabs, cards, images and switches.

The many other handler names split the rest between them, and some of them appear only once in the entire corpus. Those are specialised elements doing specialised things, such as on-scan for a barcode read or on-map-create for a map surface. Look them up when you meet the element, and do not try to learn them in advance.

Events you raise yourself
#

The framework also has a programmatic bus. A controller emits a named event, and anything that subscribed to that name receives it.

// somewhere that knows something changed
this.app.emit("work-order-saved", { wonum });

// somewhere that cares
pageInitialized(page, app) {
  this.app = app;
  this.app.on("work-order-saved", this.refresh);
}

It is useful when two parts of an app have no parent and child relationship and still need to stay in step. A detail page saves, a list somewhere else needs to know.

Use it sparingly, because the shipped apps do. Fewer than half of the apps call emit at all, and those that do use it sparingly, against a very large number of event attributes in markup. Events in this framework are overwhelmingly declarative, and the bus is the exception you reach for when the declarative path cannot connect two things.

When you do subscribe, remember that the subscription outlives the page unless you remove it. Take it down in pagePaused or keep a reference so you can.

Four things worth knowing early
#

  • A missing method is silent. The event fires, no controller answers, and the app carries on. When a button does nothing, check the spelling of the method name before anything else.
  • The -arg attribute takes a binding. on-click-arg="{item}" passes the record itself, while the same value without braces passes the bare word.
  • The same name can be handled twice. Bubbling means a datasource controller and a page controller can both answer. That is occasionally what you want and usually a surprise.
  • Not every element supports every handler. The attribute has to be one the element declares, so check the element in the reference before assuming on-click works there.

What to read next#