Controller Reference

Table of contents


Introduction

The Controller is accessed using the ctl object.


Exported identifiers

ctl

Syntax

import {ctl} from 'common/mvcs/export.js';

The ctl object contains the properties and methods used to communicate with the Controller.


ctl methods

computePage()

Syntax

ctl.computePage(pageId)

Compute a specified page. This ensure that the page's computed output state variables are up to date with respect to the page's input state variables.

If a page's computeInfo function makes a change to some state variables that are inputs to to another page compute functions it can call ctl.computePage() to update the associated output state variables immediately.

Parameters


createPage()

Syntax

ctl.createPage(info)

Create a page instance. Any methods (except getters or setters) are copied into the returned page instance.

Parameters


doSync()

Syntax

ctl.doSync()

Full synchronization of the Model and View is done immediately. The Model computations are executed until the Model stabilizes, and then the View is synchronized with the values in the updated state variables.


getSavedState()

Syntax

ctl.getSavedState()

Returns an object with the current app state for use in restoring using ctl.setSavedState().

Return value

An object containing the following properties:


onActivityChange()

Syntax

ctl.onActivityChange(callbackFn)

Calls callbackFn(isActive) when the app transitions between foreground and background processing.

Parameters


onConnectivityChange()

Syntax

ctl.onConnectivityChange(callbackFn)

Calls callbackFn(isOnline) when the device connects or disconnects from the Internet.

Parameters


onInit()

Syntax

ctl.onInit(callbackFn)

Calls callbackFn() after the controller is initialized. The function can assume:

If ctl.onInit() is called after the controller is initialized, the function is called after the current thread of execution terminates.

Parameters


onResize()

Syntax

ctl.onResize(callbackFn)

Calls callbackFn() when the viewport is resized.

Parameters


page()

Syntax

ctl.page(pageId)

Returns the page instance whose ID is pageId. Causes an assertion failure if the pageId is not found.

Parameters


resetAppValues()

Syntax

ctl.resetAppValues()

Resets all the saved state variable values to their defaults.


restore()

Syntax

ctl.restore(state)

Calling ctl.restore() with no argument causes the Controller to restore the app state from the device storage. If that is not available or the saved values are corrupted, it will reset the application state to default values. If the state parameter is present, it will restore app state from that.

Parameters


save()

Syntax

ctl.save()

Calling ctl.save() causes the Controller to save the app state to the device storage and, if online, to the server.


setSavedState()

Syntax

ctl.setSavedState(state)

Set the current application state according to state.

Parameters


startApp()

Syntax

ctl.startApp(info)

This is called by the app startup code (usually app.js) to start the Controller. The info object contains the instructions for the controller to set up the app.

Note: The app startup code must also import any modules that contain pages so that they will load. The Controller will process them when the onload event fires.

Parameters


sync()

Syntax

ctl.sync()

Schedules a full synchronization of the Model and View. The synchronization is delayed at least until the current thread of execution terminates. Subsequent calls to ctl.sync() have no effect until the synchronization is executed.


translateIds()

Syntax

ctl.translateIds(sVarState, stateVersion, versionTranslations)

Uses the versionTranslations array to map a set of old state variable IDs to new state variable IDs in the sVarState object. The versionTranslations array elements are arrays containing two values:

[version, idTranslations]

The versionString is an app version string (e.g., '2.5.1') that specifies the earliest version that the translated state variable IDs apply to. The translations are only applied if stateVersion is earlier than version. idTranslations is an array containing two or three elements: [from, to] or [from, to, valueFn]. from may be a string or a RegExp. If a string, then the properties in sVarState with key from are renamed to the string in to. If a RegExp, then all the keys in sVarState are explored, and any key matching the RegExp will be renamed to the string using:

key.replace(from, to)

The to string can refer to groupings in the from RegExp. If a valueFn is provided, it is a function that returns a transformed value for the new key. It is called with the old key value and the old key string as arguments.

For example:

const state = {
	a1: 1,
	a2: 2,
	b: 3,
	c: 4,
};
const translations = [
	['2.0.0', [
		[/^a(\d)$/, 'aa$1],
		['b', 'bb'],
		['c', 'cc', (v) => v + 1],
	]]
];
ctl.translateIds(state, '1.0.0', translations)

will change state to:

{
	aa1: 1,
	aa2: 2,
	bb: 3,
	cc: 5,
}

Parameters


upgradeSavedState()

Syntax

ctl.upgradeSavedState(state)

Upgrades previously saved state to the current version. ctl.upgradeSavedState() calls every page's upgradeSavedState() method passing the state.sVarState and state.version as arguments. If any of the calls return false, the state is considered corrupt.

If the saved data's minor release number (i.e., the first two release numbers) is greater than the current minor release number, then the data is considered incompatible.

Parameters

Return value

The method may return any of the following strings:


ctl properties

appId

The app ID passed to ctl.startApp()

Value

A string.


appTitle

The app title passed to ctl.startApp()

Value

A string.


appVersion

The app version passed to ctl.startApp(). A short ID for the app used to identify the app in the environment.

Value

A string.


debug

Set to true when extra debug logging and exception handling are required.

Value

A Boolean.


isActive

True when the app is in the foreground.

Value

A Boolean.


isOnline

True when the device is connected to the Internet.

Value

A Boolean.