MAS 9.2 series guide
Events and Event Handlers explains the event attributes and their names. This guide is about the handlers behind them. We ran every step on our test system, on the MAS 9.2 series, on a page of our Components Explorer.

Pattern 1 · One handler, many buttons#
Three buttons share one method. An argument tells the method which button it was:
<button id="fs_ctl_events_pick_a" label="teal" on-click="evPick"
on-click-arg="{{colour: 'teal', n: 1}}"/>
<button id="fs_ctl_events_pick_b" label="orange" on-click="evPick"
on-click-arg="{{colour: 'orange', n: 2}}"/>
<button id="fs_ctl_events_pick_c" label="no argument" on-click="evPick"/>Our handler writes down what it received:
| Button | First parameter | Second parameter |
|---|---|---|
with on-click-arg | the object of the attribute | the click event |
without on-click-arg | the click event | nothing |
The last row is the one to remember. A handler without an argument gets the click event of the browser, an object with type, target and many more keys. Code such as if (arg) is then true, and arg.colour is undefined.
So give every button of a shared handler an argument, also the one that needs none. A handler can then start like this sketch, which is no part of our page:
evPick(arg) {
const colour = arg && arg.colour ? arg.colour : 'none';
// ...
}Pattern 2 · A handler in a list row#
Inside a list, the argument can be the record of the row:
<list-item id="fs_ctl_events_list_item">
<button id="fs_ctl_events_list_item_btn" kind="ghost" label="pick"
on-click="evRow" on-click-arg="{item}"/>
</list-item>We pressed the button of the third row, and the handler got the third record: our trace read evRow(record WO-1003). The handler needs no index and no lookup. It has the record in its hand.
Pattern 3 · A second tap while the handler runs#
A user taps twice, out of habit or because nothing seemed to happen. Our handler takes one second:
async evSlow() {
this.slowRuns += 1;
const run = this.slowRuns;
ctlTrace(`evSlow run ${run} started`);
await new Promise((resolve) => setTimeout(resolve, 1000));
ctlTrace(`evSlow run ${run} finished`);
}We clicked the button three times: at the start, after 400 milliseconds and after 1.3 seconds. The trace showed two runs, each with its start and its end.
The click after 400 milliseconds ran nothing. The button ignored it, because its handler was still at work. The click after 1.3 seconds ran the handler again. We saw the same with a fast handler: two clicks in the same instant ran it once.
So the framework protects a button against a double tap on itself, as long as its handler is at work. For an async handler that means until its last await is done. A handler that starts a request and returns at once is free for the next tap at once, so let the handler wait for its work.
Pattern 4 · Two buttons that must not run together#
The protection ends at the button. While the slow handler ran, we clicked a second button:
evSlow run 1 started
evPick(…)
evSlow run 1 finishedThe second handler ran in the middle of the first one. For a Save and a Delete button on the same record, that is the case to prevent. A state does it. Every button that has to wait binds to it:
<state id="fs_ctl_events_busy" name="busy" type="boolean" value="false"/>
<button id="fs_ctl_events_guard" label="slow, with a busy state" on-click="evGuarded"
disabled="{page.state.busy}"/>Our handler, shown without its trace lines:
async evGuarded() {
if (this.page.state.busy) return;
this.page.state.busy = true;
try {
await new Promise((resolve) => setTimeout(resolve, 1000));
} finally {
this.page.state.busy = false;
}
}While our handler ran, the button was disabled. The finally matters: without it, one error leaves the page locked for good.
Pattern 5 · A handler that throws#
Our handler throws an error on purpose. We pressed its button. Nothing changed on screen, and the console held one line:
[ERROR]: [FSCOMPM]: [JS-Controllable]: [fs_ctl_events]: Controller event evThrow failed
on controller instance: FsEventsController. Controller events should not throw errors.
thrown in evThrowThe framework catches the error of a click handler in the same way as the error of a lifecycle hook. The user sees a button that did nothing. A handler that can fail needs its own try and catch, and a message for the user. Using Lifecycle Hooks Correctly shows that pattern.
Pattern 6 · The handler of an input#
<text-input id="fs_ctl_events_in" label="Type here" unbound="true" on-change="evChange"/>We put three letters into the field, one after the other. The handler ran three times, once for each letter. Leaving the field ran it no fourth time.
Each call got one parameter, the event of the input. Its target is the input element, so the text is in event.target.value.
on-change therefore runs on every keystroke. A handler that starts a search or a request from it should wait until the user pauses, or use the search of a datasource, which sends its request on the Enter key.
Pattern 7 · A subscription that is never removed#
A controller can listen to an event on the bus of the application. Our page subscribes in pageResumed, in two ways. The controller, shortened to the lines of this pattern:
pageInitialized(page, app) {
super.pageInitialized(page, app);
this.onClean = (data) => ctlTrace(`bus fs-clean(${describeArgs([data])})`);
}
pageResumed(page) {
page.state.visits = (page.state.visits || 0) + 1;
const visit = page.state.visits;
// never removed
this.app.on('fs-leak', () => ctlTrace(`bus fs-leak, subscribed in visit ${visit}`));
// removed in pagePaused
this.app.on('fs-clean', this.onClean);
}
pagePaused() {
this.app.off('fs-clean', this.onClean);
}We opened the page three times and then emitted both events once:
emit fs-leak
bus fs-leak, subscribed in visit 1
bus fs-leak, subscribed in visit 2
bus fs-leak, subscribed in visit 3
emit fs-clean
bus fs-clean(object {n})The first subscription ran three times, once for every visit of the page. The second one ran once.
Two details make the clean form work. The handler is kept in a field, so off can name the same function. And the subscription is made in pageResumed and removed in pagePaused, which run in pairs.
The patterns at a glance#
| Case | What we measured | What to do |
|---|---|---|
| a shared handler | without an argument, the first parameter is the click event | give every button an argument |
| a row | {item} passes the record | use it, and skip the lookup |
| a double tap on one button | the second tap runs nothing while the handler waits | write the handler as async and wait for the work |
| two buttons | the second runs during the first | bind both to a busy state |
| an error | caught, one console line, nothing on screen | try, catch and a message |
| an input | on-change runs on every keystroke | keep the handler light |
| the bus | a subscription per visit runs once more with each visit | on in pageResumed, off in pagePaused |
What to read next#
The Controller Architecture Overview shows which controllers a click reaches, and in which order. Events and Event Handlers lists the event attributes you will use most.
IBM documents the event attributes of each element in the product itself. In the Application Configuration application, open the ? menu and choose Component Documentation.
