↓ Skip to main content
An ink and watercolour drawing of a kitchen worktop. A small wooden stand holds one blank card, and on its front sit two round buttons, one teal and one orange. A hand presses the orange button with one finger. On a shelf behind stand five glass jars with teal contents, and a pencil lies on the worktop.

Cookbook: Add a Dialog to a Maximo Mobile Page

Maximo Mobile Cookbook - This article is part of a series.
Part 2: This Article
A dialog in a Maximo Mobile app is three pieces: the dialog in the page, a way to open it, and the code behind its buttons. The piece people leave out is the handler that opens it. Without that handler our dialog opened empty the first time, and showed a value the user had cancelled the second time.

MAS 9.2 series cookbook

This recipe adds a “Change priority” dialog to the work order page of the first recipe, and the dialog saves the new value to Maximo. We ran every step on our test system, on the MAS 9.2 series. The complete page, the controller and its tests are at the end as a download. Dialogs and Lookups explains the elements this recipe uses.

What you need
#

  • The page from the first recipe, or any page with a datasource that holds one record.
  • A controller for that page. Two methods go into it.

Step 1 · Put the dialog into the page
#

A dialog lives in the <dialogs> element of its page, beside the page’s content:

<dialogs id="fs_k_dialog_dialogs">
  <dialog id="fs_k_dialog_prio" title="Change priority" size="sm"
      async-primary-action="true" busy="{page.state.busy}"
      primary-action-text="Save" secondary-action-text="Cancel"
      primary-button-disabled="{page.state.newPriority === ''}"
      on-primary-action="savePriority" on-secondary-action="closeDialogs">
    <box id="fs_k_dialog_prio_c" direction="column">
      <text-input id="fs_k_dialog_prio_in" label="Priority" value="{page.state.newPriority}"/>
    </box>
  </dialog>
</dialogs>

The input binds to page state, so the record stays untouched until the user saves. The page declares that state:

<state id="fs_k_dialog_new" name="newPriority" type="string" value=""/>
<state id="fs_k_dialog_busy" name="busy" type="boolean" value="false"/>

Three attributes of the dialog carry the behaviour:

AttributeWhat it does here
async-primary-action="true"the dialog stays open while savePriority runs, and closes only when the code closes it
busy="{page.state.busy}"the dialog’s loading spinner, switched on by the method while the save runs
primary-button-disabledSave is disabled while the input is empty

Our MAF editor shows the same attributes in its properties panel, each with its type and IBM’s description:

The properties panel of the MAF editor for the dialog fs_k_dialog_prio. Under “Set”: id, title “Change priority”, size sm, async-primary-action ticked true, busy bound to page.state.busy, and primary-action-text “Save”. A line at the top says that edits go to the page file cookbook-dialog.xml.
The attributes this recipe sets on the dialog, in the properties panel of our MAF editor

Step 2 · Add a button that opens it
#

<button id="fs_k_dialog_open" kind="secondary" label="Change priority" on-click="openPriorityDialog"/>

The button calls a method of the page’s controller. A button can also open a dialog by itself, with dialog="fs_k_dialog_prio" and no code. Step 4 shows what that costs.

Step 3 · Write the two methods
#

The first method fills the input with the value of the record and opens the dialog:

openPriorityDialog() {
  const ds = this.page.findDatasource('fs_k_dialog_wo');
  const current = ds && ds.item ? ds.item.wopriority : '';
  this.page.state.newPriority = current === undefined || current === null ? '' : String(current);
  this.page.showDialog('fs_k_dialog_prio');
}

The second one runs on Save. It writes the value to the record, saves the datasource, and closes the dialog after Maximo accepted the save:

async savePriority() {
  const value = FsCookbookController.priorityFrom(this.page.state.newPriority);
  if (value === null) {
    this.app.toast('Enter a whole number from 0 to 999', 'error');
    return;
  }
  this.page.state.busy = true;
  try {
    const ds = this.page.findDatasource('fs_k_dialog_wo');
    ds.item.wopriority = value;
    const result = await ds.save();
    if (result && result.error) throw new Error(result.error.message || 'the save was refused');
    this.page.findDialog('fs_k_dialog_prio').closeDialog();
    this.page.state.lastSave = `priority ${value}`;
    this.app.toast('Priority saved', 'success');
  } catch (e) {
    this.app.toast(`Save failed: ${e.message}`, 'error');
  } finally {
    this.page.state.busy = false;
  }
}

priorityFrom turns the text into a whole number from 0 to 999, or into null. The Cancel button calls closeDialogs, which is one line in our controller: this.page.closeAllDialogs(). Our five tests cover the open, the save, the refused save and the value that is no number. In the test of the refused save the dialog stays open, so the user can correct the value and try again.

Step 4 · Build and try it
#

yarn clean && yarn build:mobile
Three phone frames. Left, the work order page with priority 2 and two buttons, “Change priority” and “Open it without the handler”. Middle, the dialog “Change priority” full screen, with the value 3 typed into its input and Cancel and Save at the bottom. Right, the page after the save: priority 3, and below the buttons the line “Last save: priority 3”.
The page, the dialog with a new value, and the page after the save

On a phone the dialog takes the whole screen, with size="sm" too. We typed 3 and tapped Save. One request went to the record and came back with 200 after 190 milliseconds. The page then showed priority 3, and a read through the API confirmed 3 on the server, changed by the user of our API key.

Then we tried the path people forget. We typed 7, tapped Cancel, and opened the dialog again, once through the method of Step 3 and once through a button with only dialog="fs_k_dialog_prio":

OpenedFirst timeAfter typing 7 and Cancel
through openPriorityDialogthe value of the recordthe value of the record
by the dialog attribute alonean empty input, Save disabled7, the cancelled value

Page state outlives the dialog. So the dialog shows what the state holds, and the handler is the place that sets it.

Two things the build does to you
#

A new controller file needs a second build. The first build after we added the controller copied the file into the app’s src folder and stopped there. The preview then failed with “Failed to resolve import”. The second build carried it into the built app.

A warm build misses a change in a page file. We added a button to the page and ran the build without a clean. The built page did not have the button. yarn clean first, every time.

The same places in an IBM app
#

In an app you duplicated from IBM, a page’s dialogs sit in the same <dialogs> element, in src/app.xml or in a page fragment under src/maml/pages. Put your own methods into src/AppCustomizations.js or a controller of your own, and leave IBM’s controllers as they are. That keeps your changes apart from the next update.

Download
#

The files of this recipe: the page we start from, the page with the dialog, the controller and its tests. The controller extends a small base class of our app that provides closeDialogs and the toast.

This recipe as a PDF, to print or to read offline.

What to read next#

The next recipe turns a field into a lookup. Dialogs and Lookups covers sizes, sliding drawers and the built-in prompts.


IBM documents the elements of this recipe in the product itself. In the Application Configuration application, open the ? menu and choose Component Documentation for dialog, button and text-input.

Maximo Mobile Cookbook - This article is part of a series.
Part 2: This Article