MAS 9.2 series guide
This guide follows Controller Basics, which introduces the four hooks to learn first. We ran every step on our test system, on the MAS 9.2 series, on pages of our Components Explorer. Every controller of those pages writes each call it gets into one trace.
The hooks of this guide#
| Hook | On the controller of | It ran |
|---|---|---|
applicationInitialized(app) | the application | once, before any page |
onDatasourceInitialized(ds) | a datasource | once, when the page of the datasource first opened |
pageInitialized(page, app) | a page | once, on the first visit |
pageResumed(page, app) | a page | on every visit, the first one included |
onBeforeLoadData(ds), onAfterLoadData(ds, items) | a datasource | around every load |
pagePaused(page, app) | a page | every time the user left the page |
dialogInitialized(dialog) | a dialog | on the first open |
dialogOpened() | a dialog | on every open |
Rule 1 · Work for every visit goes into pageResumed#
We opened a page, went on to a second page, and came back. The page controller got these calls:
page.pageInitialized(fs_ctl_trace)
page.pageResumed(fs_ctl_trace)
page.pagePaused(fs_ctl_trace)
page.pageResumed(fs_ctl_trace)pageInitialized ran once, and the return ran pageResumed alone. A page that loads its list in pageInitialized therefore shows old rows after the user comes back from a detail page. Load in pageResumed, which runs on the first visit too.
pageInitialized is the place for what the page needs once: keeping the page and the app, and setting values that stay.
Rule 2 · The rows arrive after the page hooks#
On the first open of our page, the trace held these lines for the page controller and for the controller of one datasource:
ds.onDatasourceInitialized(fs_ctl_ds)
page.pageInitialized(fs_ctl_trace)
page.pageResumed(fs_ctl_trace)
ds.onBeforeLoadData(fs_ctl_ds)
ds.onAfterLoadData(fs_ctl_ds, 10 rows)The datasource exists when pageInitialized runs, so the controller can find and configure it there. Its rows are a different matter. The load started after pageResumed, and the ten rows arrived last.
Code that needs the rows has two correct places. It can call load() and wait for it. On a datasource that is loaded already, the call returns with the rows the datasource holds:
async pageResumed(page) {
const ds = page.findDatasource('fs_ctl_ds');
await ds.load();
// ds.items is filled here
}Or it goes into onAfterLoadData on the controller of the datasource, which gets the rows as its second argument.
Rule 3 · A hook is not waited for#
A hook may be an async method. The framework calls it and goes on. One of our pages proves it: its pageInitialized returns a promise that takes 1.5 seconds and then sets a state:
pageInitialized(page, app) {
super.pageInitialized(page, app);
ctlTrace(`page.pageInitialized(${page.name})`);
if (page.name === 'fs_ctl_async') return this.slowStart(page);
return undefined;
}
async slowStart(page) {
await new Promise((resolve) => setTimeout(resolve, 1500));
page.state.ready = 'yes';
ctlTrace(`page.pageInitialized(${page.name}) finished`);
}The lines of the page controller in the trace of that page:
page.pageInitialized(fs_ctl_async)
page.pageResumed(fs_ctl_async)
page.pageInitialized(fs_ctl_async) finishedpageResumed ran while pageInitialized was still at work. The page was on screen with “Finished: no”, and 1.5 seconds later the line changed to “Finished: yes”.

So pageResumed cannot rely on anything that an async pageInitialized sets up after its first await. When a later hook needs the result of an earlier one, keep the promise and wait for it yourself:
pageInitialized(page, app) {
this.page = page;
this.app = app;
this.ready = this.setup();
}
async pageResumed() {
await this.ready;
// the setup is done here
}The page draws before either of them has finished. Bind what the user sees to a state such as a busy flag, and set it at the end.
Rule 4 · A hook that throws fails in silence#
Another page of ours throws an error in pageResumed, on every visit. We opened it, and the page drew as usual, with its texts and its state. Nothing on screen showed an error.
The framework had caught the error. The console held one line, at the level error:
[ERROR]: [FSCOMPM]: [JS-Controllable]: [fs_ctl_throw]: Controller event pageResumed failed
on controller instance: FsCtlController. Controller events should not throw errors.
thrown in pageResumedThe line names the page, the hook, the class and the message of the error. The rest of the app went on: the application controller got its own pageResumed for that page, and a second visit ran the hook again.
For the user this is the worst kind of failure. The page looks complete, and the part of the hook after the error never ran. A hook that can fail needs its own try and catch, and a message the user can see:
async pageResumed(page) {
try {
await page.findDatasource('fs_ctl_ds').load();
} catch (e) {
this.app.toast(`Loading failed: ${e.message}`, 'error');
}
}this.app is the application that the controller kept in pageInitialized.
When a page behaves as if half of its code were missing, open the console and look for “Controller event”.
Rule 5 · pagePaused runs in both directions#
The page controller got pagePaused when the user went forward to another page, and again when the user went back from it. It is the one place for what a page has to stop or to save when it leaves the screen: a timer, a scan, a draft.
The page itself stays alive after pagePaused. Its controller and its state are still there on the next visit, as AppController vs. PageController shows.
Rule 6 · A dialog has two hooks for opening#
The controller of our dialog got dialogInitialized and dialogOpened on the first open, and dialogOpened alone on every later open. Reset the form of a dialog in dialogOpened.
We closed the dialog with its button and with its close icon. In both cases the dialog controller got no call. Do the work of a close where the close is decided: in the handler of the button that closes the dialog.
Which hook for which job#
| Job | Hook |
|---|---|
| keep the app, start a service, set values for the whole app | applicationInitialized |
| keep the page, set values that stay | pageInitialized |
| load or refresh the data of a page | pageResumed |
| clear a form before the user sees it again | pageResumed, or dialogOpened for a dialog |
| configure a datasource once | onDatasourceInitialized |
| change or add to the rows after each load | onAfterLoadData |
| stop a timer, save a draft | pagePaused |
A hook of a datasource also reaches the page controller and the application controller, when they have a method of the same name. The Controller Architecture Overview shows that path.
What to read next#
The cookbook recipe Add a Dialog uses these rules in a small case: a handler that fills the dialog before it opens, and a save with its own error message. Loading and Displaying Data covers the load calls a hook uses.
IBM documents the controller hooks in its developer documentation for the Maximo Application Framework. The elements that carry a controller attribute are documented in the product itself. In the Application Configuration application, open the ? menu and choose Component Documentation.
