↓ Skip to main content
An embroidered picture of a wooden board with five metal coat hooks on a pale wall. A teal jacket hangs on the first hook and an orange scarf on the second. The other three hooks are empty.

Using Lifecycle Hooks Correctly

A lifecycle hook is a method with a fixed name. You write it, and the framework calls it at its moment: when a page opens, when data arrives, when a dialog shows. Most bugs around hooks come from a wrong idea of that moment. We measured the moments on our test pages, and two cases that are hard to see in a normal app: a hook that takes its time, and a hook that throws.

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
#

HookOn the controller ofIt ran
applicationInitialized(app)the applicationonce, before any page
onDatasourceInitialized(ds)a datasourceonce, when the page of the datasource first opened
pageInitialized(page, app)a pageonce, on the first visit
pageResumed(page, app)a pageon every visit, the first one included
onBeforeLoadData(ds), onAfterLoadData(ds, items)a datasourcearound every load
pagePaused(page, app)a pageevery time the user left the page
dialogInitialized(dialog)a dialogon the first open
dialogOpened()a dialogon 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) finished

pageResumed 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”.

Three phone frames. First, the page “a hook that takes its time” right after it opened: the trace ends with pageResumed for fs_ctl_async, and the line reads “Finished: no”. Second, the same page three seconds later: the trace has one more entry, “pageInitialized(fs_ctl_async) finished”, and the line reads “Finished: yes”. Third, the page “a hook that throws”: its trace shows pageInitialized and pageResumed on the page and on the application, and the page shows its text as usual.
A slow hook right after the open and three seconds later, and a page whose hook throws

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 pageResumed

The 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
#

JobHook
keep the app, start a service, set values for the whole appapplicationInitialized
keep the page, set values that staypageInitialized
load or refresh the data of a pagepageResumed
clear a form before the user sees it againpageResumed, or dialogOpened for a dialog
configure a datasource onceonDatasourceInitialized
change or add to the rows after each loadonAfterLoadData
stop a timer, save a draftpagePaused

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.