↓ Skip to main content
A folk art painting of two buildings side by side. On the left a large teal house with a scalloped roof and five doors in a row. On the right a small orange hut with one door. Six orange stepping stones lead away from the door of the hut.

AppController vs. PageController

Every handler you write needs a home, and most of the time the choice is between two classes: the controller of the page, or the controller of the application. Both can answer a click and both can hold values. They differ in how many of them exist, in how long they live, and in who asks them first. We measured each of these on two pages that share one controller class.

MAS 9.2 series comparison

This comparison follows the Controller Architecture Overview, which shows all four kinds of controller on one page. We ran every step on our test system, on the MAS 9.2 series, on pages of our Components Explorer.

One for the app, one for each page
#

The application names its controller once, and each page names its own. The opening tags, shortened:

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

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

Two of our pages name the same class. To see how many objects that makes, the class counts its instances. The part of the class that counts, shortened here:

let instances = 0;

class FsCtlController extends FsExplorerController {
  constructor() {
    super();
    instances += 1;
    this.instance = instances;
    this.resumes = 0;
  }

  pageResumed(page) {
    const p = page || this.page;
    this.resumes += 1;
    p.state.visits = (p.state.visits || 0) + 1;
  }
}

The first page showed instance 1 and the second page showed instance 2. Each page gets an object of its own, also when two pages name one class. A field such as this.resumes therefore belongs to one page.

The application controller exists once. Its first call, applicationInitialized, was the first line of our trace, before any page.

Three phone frames. First, the second page after it opened: the trace ends with pageInitialized and pageResumed for fs_ctl_two, and the line reads controller instance 2, visits 1, count on the application 3. Second, back on the first page: the trace ends with pagePaused for fs_ctl_two and pageResumed for fs_ctl_trace, and the line reads controller instance 1, visits 2, count on the application 3. Third, the second page again: instance 2, visits 2, count 3, and the trace shows pageResumed for it without a pageInitialized.
Two pages, one controller class: two instances, and each page keeps its visits

What survives when the user leaves
#

We went from the first page to the second, back, and to the second again, and read the line under the trace each time:

StepInstanceVisits of that pageHooks the page controller got
the first page opens11pageInitialized, pageResumed
the second page opens21pageInitialized, pageResumed
back to the first page12pageResumed
the second page again22pageResumed

A page that the user leaves stays alive. Its controller object stayed the same, its fields kept their values, and its page state kept counting. pageInitialized ran once per page, and each return ran pageResumed alone.

So a value in a page controller or in page state is still there when the user comes back. That is useful for a filter the user set. It is a trap for a form: the values of the last visit are still in it, until pageResumed clears them.

Where state lives
#

Both controllers reach two stores. page.state belongs to one page. app.state belongs to the application, and every page reads the same values. A button on our pages counts on the application:

ctlCount() {
  this.app.state.ctlCount = (this.app.state.ctlCount || 0) + 1;
}

We pressed it three times on the first page and opened the second page. The second page read 3. The number of visits, which each page keeps in its own state, read 1 there.

page.stateapp.state
Who reads itthe page and its controllerevery page, every controller
How long it livesas long as the app runs; the page keeps it between visitsas long as the app runs
Use it forwhat one screen shows: a tab, a filter, a busy flagwhat pages share: the record the user picked, a setting

State Management covers the declaration of state and its bindings.

Who answers a click
#

A button names a method, and the two controllers are asked in a fixed order. We gave buttons of the first page four names:

on-clickThe method exists onWhat ran
ctlPageOnlythe page controllerthe page controller
ctlAppOnlythe application controllerthe application controller
ctlBothboththe page controller, then the application controller
ctlNowherenonenothing

A click goes to the page controller first and then on to the application controller. Each of the two that has the method runs it. So the application controller is the place for a handler that many pages need: write it once there, and a button on any page reaches it.

Note the third row. A method name that exists on both runs twice, once on each. When a page should answer in its own way, give its method a name of its own.

The last row fails in silence. Our MAF editor checks every handler against the controllers of the app and marks the one that no class has. It leaves ctlAppOnly unmarked, because the application controller has that method:

The code view of the MAF editor, lines 27 to 34 of the page. Lines 27 to 33 are buttons with an on-click attribute: ctlBoth, ctlPageOnly, ctlAppOnly, ctlDsOnly, two with the prefix fs_ctl_ds, and ctlNowhere. Line 33, the button with on-click ctlNowhere, carries a yellow warning triangle and a wavy line under the method name.
Seven handlers, one without a method: the mark of our MAF editor on line 33

Its finding reads: “No controller of this app has a method ‘ctlNowhere’ for on-click.”

The application controller hears every page
#

The page hooks do not end at the page. Our application controller has the three methods too, and it got each of them for every page we opened, right after the page’s own controller:

page.pagePaused(fs_ctl_trace)
app.pagePaused(fs_ctl_trace)
page.pageInitialized(fs_ctl_two)
app.pageInitialized(fs_ctl_two)
page.pageResumed(fs_ctl_two)
app.pageResumed(fs_ctl_two)

The hook gets the page as its argument, so one method can serve all pages. In our controller, ctl adds a line to the trace and isCtl picks the pages of this demo:

// AppController
pageResumed(page) {
  if (isCtl(page)) ctl(this.app, `app.pageResumed(${page.name})`);
}

This is the place for work that belongs to every visit of every page: a check, a log line, a value that each page needs fresh. The application controller got its call also on a page whose own pageResumed threw an error, as Using Lifecycle Hooks Correctly shows.

Which one to use
#

You writePut it into
the handler of a button that exists on one pagethe page controller
a handler that buttons on several pages sharethe application controller
the load of a page’s data on each visitpageResumed of the page controller
work for every visit of every pagepageResumed of the application controller
setup that the whole app needs onceapplicationInitialized of the application controller
a value that one screen showspage.state
a value that pages shareapp.state

Start in the page controller. Move a method to the application controller on the day a second page needs it.

What to read next#

Using Lifecycle Hooks Correctly covers what a hook may do and what happens when it takes long or fails. App vs Page vs Datasource Scope covers the same question for datasources: where to declare one, and how code finds it.


IBM documents the attribute controller in the product itself. In the Application Configuration application, open the ? menu and choose Component Documentation for maximo-application and page.