↓ Skip to main content
One large faceted orange cube on a dark plinth, with four arms reaching out to four teal blocks, among scattered low-poly shapes

Controller Basics

Your markup says what the screen holds, and your controller says what happens. The framework owns the timing, and it calls your methods at moments it has already decided on, which is the part that surprises people. Learn which moments those are and most controller confusion goes away.

MAS 9.2 series framework 3.2.8xx 4 hooks to learn

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.

What a controller is
#

A controller is a plain JavaScript class that you name in your markup, and the framework builds it, hands it the objects it belongs to, and calls methods on it when something happens.

<page id="wolist" controller="ListPageController">

That one attribute is the whole wiring, with no import, no registration call and no base class you have to extend. The framework looks for methods by name, and it calls the ones it recognises.

Two things follow from that design. Your code never drives the lifecycle, so there is no main function and no place where you start things off. And a method the framework does not recognise is simply never called, with no warning at build time and no error at runtime.

Where a controller attaches
#

You will meet four places, and they cover the overwhelming majority of real work.

Attached toWritten asWhat it is for
The application<maximo-application controller="AppController">one per app, for global setup
A page<page controller="ListPageController">one per page, for that screen
A datasource<maximo-datasource controller="WoDataController">one per datasource, for its data
A dialog<dialog controller="StatusController">one per dialog, for that popup

In the shipped apps those four element forms carry the large majority of all controller= attributes. The rest sit on further element kinds, including drawers, lookups and flyouts, and the members-only companion to this post lists every one of them.

The four methods to learn first
#

The framework declares close to thirty lifecycle methods in total. Four of them account for most of the controller methods in IBM’s own apps, so these are the ones to know by heart.

pageInitialized(page, app) runs once, the first time a page is shown. Keep references here and set up anything that survives for the life of the page, while anything that has to be fresh belongs elsewhere.

pageResumed(page, app) runs every time the page becomes visible, including the first time. Load and refresh your data here, because this is the method that matters most, for the reason the next section gives.

onDatasourceInitialized(ds, owner, app) runs once, when a datasource is built. Store the datasource here, configure it, and register whatever the rest of your controller will need later.

onAfterLoadData(ds, items, query) runs after every successful fetch. Change what came back, derive display values, and adjust the list before it reaches the screen.

Which hook runs on which visit Which hook runs on which visit
Which hook runs on which visit
class ListPageController {
  pageInitialized(page, app) {
    this.page = page;
    this.app = app;
  }

  pageResumed() {
    // runs on every visit, including the first
    this.page.findDatasource("woDS").load();
  }
}

The mistake worth knowing about
#

A list loads correctly the first time, and then the user opens a record, changes it, comes back, and the list still shows the old values. Nothing throws and nothing logs.

The cause is almost always that the load sits in pageInitialized, which runs once in the life of the page and never again. Coming back triggers pageResumed instead.

You can watch the two hooks count. On our test page each one adds one to its own number in page state. We opened the page, went to another page and came back, twice:

Three frames of Maximo Mobile at tablet width, on the page “pageInitialized and pageResumed”. In all three, pageInitialized ran 1 time. pageResumed ran 1 time, then 2 times, then 3 times.
pageInitialized runs once, pageResumed on every visit, in Maximo Mobile at tablet width, in the preview of our MAF editor
pageInitialized(page, app) {
  super.pageInitialized(page, app);
  page.state.initCount = (page.state.initCount || 0) + 1;
}

pageResumed(page) {
  page.state.resumeCount = (page.state.resumeCount || 0) + 1;
}

A load in pageInitialized therefore runs on the first visit only, and every later visit shows what was loaded then.

Because pageResumed also runs on the first visit, you rarely need both, so put the load in pageResumed and delete the one in pageInitialized. IBM’s own apps do this: they define pageResumed almost as often as pageInitialized, which is far closer than the naming suggests.

Four things that catch people out
#

  • Your hook is not awaited. The framework calls it and carries on, so shipped code wraps the body in an immediately-invoked async function when it needs to wait for something.
  • Events bubble. An unhandled event travels from datasource to page to application, and a method name that exists in two of those places runs in both. Keep your names distinctive.
  • A method the framework does not know is never called. There is no warning. If a hook seems dead, first check that the name is really one the framework calls.
  • Dialogs have their own pair. dialogInitialized runs on the first open and dialogOpened runs every time, which is exactly the page pattern again.

What to read next#

  • The Complete Controller Lifecycle is the members-only companion. It lists every method with the counts behind this post, the exact firing order for startup, navigation and dialogs, and the three places the usual documentation and the shipped code disagree.
  • Datasource Basics for what your datasource controller is holding.
  • Pages and Navigation for what drives the page methods above.