Page Overview

Table of Contents


Introduction

Pages form the bulk of an app. A Page instance contains the following:

The Nav page

The built-in Nav page is always open and provides the base navigation elements that the user manipulates to open and close pages. The Nav page has a separate nav object that the Controller, View, and other pages use to interact with it. The Nav page manages the page stack, representing a stack of pages from the bottommost to the topmost. There are three types of pages:

It is possible for the same page to appear more than once in the stack as one or more popup pages or as a base page and popup pages. Overlay pages typically appear only at the top of the stack.

Pages can be opened programmatically by other pages by calling:

The base page is always at the bottom of the page stack, so there can only be one base page. When a new base page is opened, the old one is closed. However, the same page can be opened as both a base page and/or one or more popup pages.

When a page opens another page programmatically, it can specify an argument to adjust the target page's state. The argument is passed to the target page's open() method.

Page initialization

A page is usually created by calling ctl.createPage() when its code file is loaded by the app startup code. The app startup code calls ctl.startApp() which usually provides instructions to the Nav page about page navigation layout and which page will be displayed initially.

After the app is fully loaded, the Controller gathers all the created pages and creates and publishes their state variables in the global sv namespace object. It then retrieves any state saved in the device storage and asks each page to upgrade the saved to the current app version using the upgradeSavedState() method. After that, the Controller asks each page to restore its state variables using the setSavedState() method. The default setSavedState() method simply sets the page’s state variables to the values found in the saved state, but the page can override this. For example, examine the state for corruption before setting any state variables.

Once all the page state variables have been restored, the Controller calls each page’s init() method. The page can use this method to do any setup when the app is fully loaded, and all the public state variables are available. Once all the pages are initialized, the Controller runs the Model to set up the app state variables according to the saved state and then initializes the View according to instructions provided to ctl.startApp().

Page states

Pages can be in the following states:

After initialization, each page is in the Closed state. The Nav page automatically opens the initial page by asking the Controller to call the page's open() method. The Nav page does the same for pages selected by the user. After that, the Nav page calls the page’s component() or render() method to provide the Component or Template to build a Component tree that represents the page in the View. The Nav page then instantiates the Component or Template in the DOM.

A page moves back to the Closed state when it is dismissed. The Nav page removes it from the top the stack and calls the page’s close() method to allow it to clean up its state variables for the next instantiation.

A page moved to the Deactivated state when it is opened but is no longer the active page on the top of the stack. Deactivated pages cannot receive user inputs and its input state variables can’t be changed by the user. Their View elements remain in the DOM to preserve DOM state (e.g. scroll offsets), though they may not be displayed when they are completely obscured. When a page is deactivated, the Controller calls its deactivate() method, which returns an object representing the page's current instantiation. If the same page is reopened somewhere above the deactivated instance, the new instance will use the same state variables as the older instance. When the pages above the older instance close, the information returned by the deactivate() method is passed to the page’s reactivate() method to restore the instance state.

Model computation

Each page has an array of computations that are evaluated when the Model is executed. Each computation object specifies a computation function and three arrays containing state variable IDs or state variable objects: inputs, outputs, and referenced. The arrays may reference state variables on other pages.

The inputs array contains the list of state variables that should trigger the computation when any of them have changed (according to each state variable’s change criteria) since the last time the computation was run. The outputs array contains the list of state variables that are reset to their default value before the computation is run. This allows a computation function to return early if an error is detected without having to reset these state variables from previously computed values. The referenced array contains the list of state variables that are used by the computation function but don’t trigger the computation and should not be reset before the computation is executed. All the state variable instances contained in the three arrays are added to the io namespace which is passed to the computation function. Using this namespace instead of the global sv namespace to access state variables within the function will cause a reference error if the state variable is not present in any of the three arrays. This will help ensure that all state variables used by the function are accounted for.

In each cycle of Model execution, each page’s array of computations is evaluated in order, and any triggered computations are executed. In general, it’s better to ensure that individual computations that depend on the output of other computations are placed after those computations in the array. In addition, The Model attempts to order page evaluation so that pages that provide outputs to a page are evaluated before that page. However, cycles are allowed, and the Model will repeat evaluating all pages until all inputs are unchanged since the last time their dependent computation was evaluated. If the Model runs an excessive number of cycles, it will terminate with an assertion failure.

Help and tips

If desired, each page can have its own help topic by providing a renderHelp() method. The renderHelp() method returns a template containing the page help information. The help topic will be the page's title. A page can also provide additional help topics by calling help.addTopic() (see Help Page Reference).

A page can also have tips. Tips are popup messages that provide helpful hints. Some tips are displayed the first time a page is opened and others are displayed when a page is opened sometime later. For the latter type, tips display is throttled to minimize annoyance. An individual tip is only displayed once, but they are always available at the end of the page’s help page. See Page Reference.

Page creation

Pages are created by calling ctl.createPage(info). The info object contains the following properties: