↓ Skip to main content
A paper quilling picture of an old switchboard panel with nine round teal sockets in a row. Three orange cords are plugged into three of the sockets and hang down. Coiled paper ornaments frame the panel.

Controller Methods Cheatsheet

A controller is a plain JavaScript class. The framework calls its methods at fixed moments, and your markup calls them by name. This page lists those moments, the path a call takes, and the calls a controller makes itself. Keep it open while you write, and follow a link when a line needs its reasons.

MAS 9.2 series cheatsheet

Every behaviour on this page is measured on our test system, on the MAS 9.2 series. The calls in the code blocks run in the controllers of our Components Explorer, and the two patterns at the end come from the guides.

Where a controller is named
#

ControllerNamed onHow many objects
application controller<maximo-application controller="…">one for the app
page controller<page controller="…">one for each page, also when two pages name the same class
datasource controller<maximo-datasource controller="…"> or <json-datasource controller="…">one for each datasource that names a class
dialog controller<dialog controller="…">one for each dialog that names a class

The guides for this section are Controller Architecture Overview, AppController vs. PageController.

The lifecycle hooks
#

HookOn the controller ofRuns
applicationInitialized(app)the applicationonce, before any page
onDatasourceInitialized(ds)a datasourceonce, when its page first opens
pageInitialized(page, app)a pageonce, on the first visit
pageResumed(page, app)a pageon every visit, the first one included
onBeforeLoadData(ds)a datasourcebefore every load
onAfterLoadData(ds, items)a datasourceafter every load, with the rows
pagePaused(page, app)a pageevery time the user leaves the page, forward or back
dialogInitialized(dialog)a dialogon the first open
dialogOpened()a dialogon every open

The rules that go with the table:

  • A hook is not waited for. An async pageInitialized of 1.5 seconds finished after pageResumed had run.
  • A hook that throws is caught. The page draws, and the console holds one line that contains “Controller event”.
  • The rows arrive after the page hooks. The first load of a bound datasource starts after pageResumed.
  • A page that the user leaves stays alive. Its controller, its fields and its state are there on the next visit.
  • A dialog controller gets no call on a close. Do the work of a close in the handler of the button.

The guide for this section is Using Lifecycle Hooks Correctly.

Which hook for which job
#

JobHook
keep the app, 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 draft, remove a subscriptionpagePaused

Who gets a call
#

A hook and a click travel the same way: from the controller they belong to, outward.

The call starts atIt reaches, in this order
a hook of a datasourcedatasource controller, page controller, application controller
a hook of a pagepage controller, application controller
on-click="method"datasource controller, page controller, application controller
on-click="datasourceId:method"the controller of that datasource, then page and application controller
  • Every controller on the path that has a method of that name runs it. A name that exists twice runs twice.
  • A button of any page reaches a method that only the application controller has.
  • A name that no controller has runs nothing, and nothing tells the user.

The guide for this section is Controller Architecture Overview.

What a handler receives
#

MarkupFirst parameterSecond parameter
on-click="m" on-click-arg="{{colour: 'teal'}}"the objectthe click event
on-click="m" on-click-arg="{item}" in a listthe record of the rowthe click event
on-click="m"the click eventnothing
on-change="m" on a text-inputthe event of the input, once for each keystrokenothing
  • A button ignores a second tap while its own async handler is at work.
  • Another button does not wait. Bind both to a state such as page.state.busy when they must not run together.
  • A handler that throws is caught in the same way as a hook.

The guide for this section is Event Handling Patterns.

The calls of every day
#

The base class of our controllers keeps the page and the app in pageInitialized:

pageInitialized(page, app) {
  this.page = page;
  this.app = app;
}

With these two, the lines below are most of what a controller does. Each call runs in our lab app, with these values or with others:

// a datasource of the page, or one of the application
const ds = this.page.findDatasource('fs_d_load_free');
const shared = this.app.findDatasource('fs_l_panels_ds');

// state: one page, or the whole app
this.page.state.busy = true;
this.app.state.ctlCount = (this.app.state.ctlCount || 0) + 1;

// go to another page, with values for it
this.app.setCurrentPage({name: 'fs_c_nav_list', params: {reload: true}});

// open a dialog of the page, and close it
this.page.showDialog('fs_k_dialog_prio');
this.page.findDialog('fs_k_dialog_prio').closeDialog();

// tell the user
this.app.toast('Saved', 'success');
this.app.toast(`Save failed: ${e.message}`, 'error');

// the event bus: keep the handler, so that off can name it
this.app.on('fs-clean', this.onClean);
this.app.off('fs-clean', this.onClean);
this.app.emit('fs-clean', {n: 1});
StoreWho reads itUse it for
page.statethe page and its controllerwhat one screen shows: a tab, a filter, a busy flag
app.stateevery page and every controllerwhat pages share: the picked record, a setting
a field such as this.countthis one controller objectvalues that no markup binds to

The guides for this section are Pages and Navigation, Understanding State Management, Datasource Cheatsheet.

Two patterns to copy
#

A hook that can fail, with a message for the user, as the hooks guide prints it. The load is the line that may throw:

async pageResumed(page) {
  try {
    await page.findDatasource('fs_ctl_ds').load();
  } catch (e) {
    this.app.toast(`Loading failed: ${e.message}`, 'error');
  }
}

A subscription that is removed when the page leaves the screen:

pageResumed() {
  this.app.on('fs-clean', this.onClean);
}

pagePaused() {
  this.app.off('fs-clean', this.onClean);
}

Without the off, the handler ran once more for every visit of the page.

This page as a PDF, to print or to keep beside the keyboard.


IBM documents the attribute controller and the event attributes in the product itself. In the Application Configuration application, open the ? menu and choose Component Documentation. The controller hooks are described in IBM’s developer documentation for the Maximo Application Framework.