↓ Skip to main content
Paper quilling on an off-white ground: three round coils of rolled paper strips in a row, growing in size from left to right. The two smaller coils are teal and the largest is orange. One orange paper strip runs in a wave through the centre of all three.

Controller Architecture Overview

An app of any size has four kinds of controller: one for the application, one per page, and one for each datasource and dialog that needs code. The usual picture shows them as four separate boxes. We let all four write every call they get into one list. The list shows something the boxes hide: one call often visits three controllers, one after the other.

MAS 9.2 series overview

This overview builds on Controller Basics, which explains what a controller is and names the methods to learn first. We ran every step on our test system, on the MAS 9.2 series, on a page of our Components Explorer, a small Maximo Mobile app we built for these articles.

Four controllers on one page
#

The page of this article has all four kinds. Each one is a plain JavaScript class, named by the attribute controller. The four opening tags, shortened:

<maximo-application controller="AppController" id="FSCOMPM">

<page id="fs_ctl_trace" controller="FsCtlController" title="Controllers: who gets which call">

<json-datasource id="fs_ctl_ds" src="fs-workorders.js" id-attribute="_id" pre-load="true"
    selection-mode="none" controller="FsCtlDsController"/>

<dialog id="fs_ctl_dlg" title="A dialog with its own controller" size="sm"
    controller="FsCtlDialogController" primary-action-text="Close">
ControllerNamed onHow many
application controller<maximo-application>one for the app
page controller<page>one for each page that names a class
datasource controller<maximo-datasource> or <json-datasource>one for each datasource that names a class
dialog controller<dialog>one for each dialog that names a class

Every method of these four classes writes one line into a shared list, with the controller in front: app., page., ds. or dialog.. The page shows the end of the list.

Three phone frames of the page “Controllers: who gets which call”. Left, the end of the trace after the page opened: onDatasourceInitialized for the second datasource, on the datasource and on the page. Then pageInitialized and pageResumed, each on the page and on the application. Then, for each datasource, onBeforeLoadData on the datasource and onAfterLoadData on the datasource, the page and the application. Middle, the cleared trace after a click on ctlBoth: ds.ctlBoth, page.ctlBoth, app.ctlBoth. Right, the cleared trace after three other clicks: app.ctlAppOnly, ds.ctlDsOnly, then ds.ctlBoth, page.ctlBoth and app.ctlBoth.
One trace for four controllers: the page opens, and three kinds of click

One call, up to three controllers
#

We wrote the same method on more than one controller. onAfterLoadData is a hook of a datasource, and our page has it on three classes:

// FsCtlDsController, the datasource controller
onAfterLoadData(ds, items) {
  ctlTrace(`ds.onAfterLoadData(${ds.name}, ${(items || []).length} rows)`);
}

// FsCtlController, the page controller
onAfterLoadData(ds) {
  ctlTrace(`page.onAfterLoadData(${ds ? ds.name : ''})`);
}

// AppController, the application controller
onAfterLoadData(ds) {
  if (isCtl(ds)) ctl(this.app, `app.onAfterLoadData(${ds.name})`);
}

ctlTrace, ctl and isCtl are small helpers of our demo. The first two add a line to the list, and the third says whether a page or a datasource belongs to these pages.

Then we opened the page for the first time and read the list, from the first call for this page on. The page has two datasources, fs_ctl_ds and fs_ctl_lazy:

 1. ds.onDatasourceInitialized(fs_ctl_ds)
 2. page.onDatasourceInitialized(fs_ctl_ds)
 3. ds.onDatasourceInitialized(fs_ctl_lazy)
 4. page.onDatasourceInitialized(fs_ctl_lazy)
 5. page.pageInitialized(fs_ctl_trace)
 6. app.pageInitialized(fs_ctl_trace)
 7. page.pageResumed(fs_ctl_trace)
 8. app.pageResumed(fs_ctl_trace)
 9. ds.onBeforeLoadData(fs_ctl_ds)
10. ds.onAfterLoadData(fs_ctl_ds, 10 rows)
11. page.onAfterLoadData(fs_ctl_ds)
12. app.onAfterLoadData(fs_ctl_ds)
13. ds.onBeforeLoadData(fs_ctl_lazy)
14. ds.onAfterLoadData(fs_ctl_lazy, 8 rows)
15. page.onAfterLoadData(fs_ctl_lazy)
16. app.onAfterLoadData(fs_ctl_lazy)

Two rules are in this list.

A call starts at the controller it belongs to and then moves outward. A hook of a datasource went to the datasource controller, then to the page controller, then to the application controller. A hook of the page went to the page controller and then to the application controller. Every controller on the path that has a method of that name ran it.

The datasources of a page are built before the page’s own hook. Lines 1 to 4 come before pageInitialized. The rows loaded after pageResumed.

The application controller therefore hears about every page. Ours got pageInitialized, pageResumed and pagePaused for each page we opened, after the page’s own controller.

A click takes the same path
#

An event attribute such as on-click names a method. We gave buttons of the page seven different names and read which controller ran:

on-clickA method of that name exists onWhat ran, in this order
ctlBothdatasource, page and application controllerdatasource, page, application
ctlPageOnlypage controllerpage
ctlAppOnlyapplication controllerapplication
ctlDsOnlydatasource controllerdatasource
fs_ctl_ds:ctlDsOnlydatasource controllerdatasource
fs_ctl_ds:ctlBothall threedatasource, page, application
ctlNowhereno controllernothing

Four things follow from the table.

A click runs on every controller that has the method, and the order is again datasource, page, application. A name that exists twice runs twice. Keep method names distinct, unless you want both to run.

A button of a page reaches the application controller. ctlAppOnly exists only there, and the click on a page ran it.

A button reaches a datasource controller without naming the datasource. The prefix fs_ctl_ds: names one datasource, and the call still moved on to the page and the application afterwards. Our page has two datasources with the same controller class, and the plain ctlBoth ran on one of them, the first one of the page.

A name that no controller has runs nothing. The page stays as it is, and nothing tells the user. Events and Event Handlers shows that case, and AppController vs. PageController shows how our MAF editor marks it.

The dialog controller
#

The dialog of the page has a class of its own. On the first open it got dialogInitialized and then dialogOpened. On every later open it got dialogOpened alone:

dialog.dialogInitialized(fs_ctl_dlg)
dialog.dialogOpened(fs_ctl_dlg)

That is the pattern of a page again: one method for the first time, one for every time. A dialog that shows a form resets it in dialogOpened. The cookbook recipe Add a Dialog shows a dialog with its handlers on the page controller, which is the simpler choice for a small dialog.

Where your code goes
#

The path of a call gives a plain rule: put code at the innermost controller that has everything it needs.

Code aboutGoes intoBecause
the rows of one datasource: computed values, a change after each loadthe datasource controllerit gets the hooks of that datasource first
one screen: its buttons, its dialogs, its statethe page controllerone instance per page, with the page at hand
every page: a common handler, a check on each visitthe application controllerit hears the page hooks of all pages, and a click reaches it from anywhere
one dialog with real logica dialog controllerit has the two hooks of the dialog

What to read next#

AppController vs. PageController compares the two controllers you write most often: instances, state and the lookup of a handler. Using Lifecycle Hooks Correctly covers what a hook may do, and what happens when it takes long or throws.


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.