↓ Skip to main content
Three open boxes on a table drawn in ink and watercolour, a large orange one, a middle teal one and a small rose one, each holding a single round token

Understanding State Management

Every app holds values that are neither in the database nor on the screen: which tab is open, whether the form has unsaved edits, which record the user just tapped. Maximo Mobile gives you three places to keep them, and choosing wrongly is cheap on day one and expensive six months later. Here is what each place is for.

MAS 9.2 series framework 3.2.8xx 3 mechanisms

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.

The three places
#

MechanismScopeHow long it livesGood for
app.statethe whole applicationthe app sessionuser context, feature flags, values several pages need
page.stateone pagethat page’s lifecycletabs, edit mode, loading flags, unsaved-change markers
page.paramsthe page you navigate toone navigationthe record key and the record URL

You declare application and page state in markup, inside a <states> block, and where that block sits decides the scope. A block inside <maximo-application> gives you application state, and a block inside a <page> gives you page state.

<page id="wolist">
  <states>
    <state name="selectedTab" value="0" type="number"/>
    <state name="isEditing" value="false" type="boolean"/>
  </states>
</page>

Params work differently, because you never declare them and the framework hands them to the target page at the moment you navigate.

this.app.setCurrentPage({
  name: "workOrderDetails",
  params: { wonum: item.wonum, href: item.href },
});

Reading state back
#

In markup you read state through a binding, and in a controller you read it as an ordinary property.

<button label="Save" disabled="{!page.state.isEditing}"/>
pageResumed() {
  this.page.state.isEditing = false;
  const url = this.page.params.href;
}

Writing to state is enough to update the screen, because anything bound to that value re-renders on its own and nothing in your code has to ask for it.

In the sequence below, Edit sets page.state.isEditing to true. The note becomes editable and Save becomes active. Save sets it back to false, and the page returns to how it started. No code touches the buttons.

Three frames of Maximo Mobile at tablet width, on the page “page.state: Edit and Save”. First, isEditing is false: the Note is read-only, Edit is active and Save is grey. Second, after a tap on Edit, isEditing is true: the Note can be edited, Edit is grey and Save is active. Third, after a tap on Save, isEditing is false again.
page.state drives the buttons: Edit and Save, in Maximo Mobile at tablet width, in the preview of our MAF editor
<state id="fs_c_state_editing" name="isEditing" type="boolean" value="false"/>

<text-input id="fs_c_state_input" label="Note" value="{page.state.note}"
            readonly="{!page.state.isEditing}"/>
<button id="fs_c_state_edit" kind="secondary" icon="carbon:edit" label="Edit"
        disabled="{page.state.isEditing}" on-click="setState"
        on-click-arg="{{name: 'isEditing', value: true}}"/>
<button id="fs_c_state_save" kind="primary" icon="carbon:save" label="Save"
        disabled="{!page.state.isEditing}" on-click="finishEdit"/>
finishEdit() {
  this.page.state.isEditing = false;
  this.app.toast('Saved', 'success');
}

The question that settles it
#

Ask how long the value has to survive.

  • It matters only while this screen is open. Use page.state. This covers most of what you will write.
  • The next page needs it. Use page.params, and pass the record key and its URL.
  • A page three steps away needs it, or the whole app does. Use app.state.
Which place to keep a value Which place to keep a value
Which place to keep a value

IBM’s own apps land heavily on the first answer. They declare far more state at page scope than at application scope, and page state is referenced more often than application state in markup and in controller code alike.

The trap sits in the second answer, because params reach the page you navigate to and no further. A value that page B receives is gone by the time the user reaches page C. When you need it deeper than one hop, it belongs in application state.

Three things worth knowing early
#

Declare your state, even though you do not have to. A controller can create state just by assigning to a name, and that works. The value then has no type, no default and no trace in the markup for the next person reading the app. In the shipped apps most of the state names that controllers assign were never declared anywhere, which is exactly how an app becomes hard to hand over.

Application state never gets cleaned up. It lives for the whole session, so every flag you put there is permanent global memory. Reset page-scope flags in pagePaused and keep the application scope for things that genuinely span the app.

The framework gives you state too. The single most referenced piece of state in the shipped apps is app.state.screen.size, which the framework maintains and which drives responsive layout. networkConnected and pageLoading are there as well, so check for an existing value before declaring your own.

What to read next#

  • State Management Patterns is the members-only companion. It carries the full measurements, the four habits the counts expose, the framework-provided state names with their usage, and the patterns worth copying.
  • Controller Basics for the methods that read and write all of this.
  • Data Binding for how a binding reaches a state value.