↓ Skip to main content
A paper quilling picture of five teal compasses in a row. All five needles point to the right, toward one orange horseshoe magnet at the end of the row.

Reactive Updates: What the Screen Follows

In a Maximo Mobile app you never tell the screen to redraw. You change a value, and every label, field and list that shows this value follows. It works so well that a developer stops thinking about it, until one value changes and the screen stays as it was. This page shows where the line runs.

MAS 9.2 series core concept

We measured everything on this page on our test system, on the MAS 9.2 series, in the browser preview of our Components Explorer. The page of the measurement has one button for each kind of change, and each button changes one thing only.

The idea in one paragraph
#

The pattern is called observer. A value is observed, and everything that reads it is told when it changes. The apps get this from MobX, a JavaScript library: IBM’s apps name mobx and mobx-react among their dependencies. For a developer of an app the rule is short. What lives in a state is observed. A binding in the XML reads it, and the framework keeps the two in step.

What we changed
#

The controller of our page has one method for each change:

// a state
stateStep() {
  this.page.state.count += 1;
}

// a field of the controller
fieldStep() {
  this.count += 1;
}

// a value inside an object that lives in a state
boxInside() {
  this.page.state.box.a += 1;
}

// a list that lives in a state
listPush() {
  this.page.state.list.push('more');
}

The page shows each value with a label, such as label="count = {page.state.count}".

What the screen followed
#

The changeDid the screen follow?
a state gets a new valueyes
an expression in a binding, such as {page.state.count * 2}yes
a value inside an object in a stateyes
a new key on that objectyes
the object is replaced by a new oneyes
push() into a list in a stateyes
the list is replaced by a new oneyes
a field of the controller, this.countno
Two screens of the lab page after the same clicks. First: the label reads count = 2 and count times two = 4, the line for the field of the controller still reads 0, and box.a reads 2. Second, further down: list.length reads 2, and the line “What the code holds” shows count 2, field 2, shown 0, box with a = 2 and a list with two entries.
After two clicks on the first two buttons and one click on the next two: the states moved on the screen, and the field moved only in the code

The two frames together are the proof for the last row. We clicked twice on the button for the field. The code holds the value 2, as the last line of the second frame shows. The label for the field in the first frame still reads 0, because no state changed.

Three things we did not expect
#

A change deep inside an object counts. We changed box.a and left box itself alone, and the label followed. A developer who comes from other frameworks may expect that only a new object is seen. The same holds for push() into a list.

A new key counts too. The object had no key b when the page opened. We added it later, and the label that reads box.b showed the value at once.

A missing value showed a number in braces. Before the key existed, the label read box.b = {1}. It is the second binding of that label, and it had no value yet. A number in braces on a screen is therefore worth a look at the binding behind it.

The line: a state, or a field
#

A field of a controller is a normal JavaScript property. Nothing observes it, and our label never saw it change. That makes it the right place for values that only the code needs: a counter, a timer id, a cache.

A value that the screen has to show belongs in a state. If you have it in a field, copy it into a state at the moment it changes:

fieldToState() {
  this.page.state.shown = this.count;
}
The value is forKeep it in
one screen: a tab, a flag, a textpage.state
several pagesapp.state
the code onlya field of the controller

A datasource has states of its own, such as loading and hasData, and a label can bind to them in the same way. Loading and Displaying Data measures them.

Check it yourself
#

  1. Add a label with label="n = {page.state.n}" to a page.
  2. In a handler, write this.page.state.n = (this.page.state.n || 0) + 1, and click. The label counts.
  3. Change the handler to this.n = (this.n || 0) + 1, and click again. The label stays.

What to read next#

Understanding State Management covers the three places a state can live. Data Binding lists the forms of a binding, and App vs Page vs Datasource Scope says which state a binding finds.


MobX documents the observer pattern and its rules at mobx.js.org. IBM documents the state element in the product itself, in the Application Configuration application under Component Documentation.