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

computeModel()

Syntax

ctl.computeModel()

The Model computations are executed until the Model stabilizes.


computePage()

Syntax

ctl.computePage(pageId)

Compute a specified page. The page's computations are run without first computing the pages that supply its inputs (its precedents). 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:


hasPage()

Syntax

ctl.hasPage(pageId)

Returns true if a page with ID pageId exists. Unlike ctl.page() it does not cause an assertion failure if the page is not found.

Parameters


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


pages()

Syntax

ctl.pages()

Returns an array of all the page instances.


resetAppValues()

Syntax

ctl.resetAppValues()

Resets every input state variable to its default (using setDflt()), calls setSavedState(null) on every page so that each page can react to the reset, and then schedules a ctl.sync().


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.

The Controller calls ctl.save() itself at the end of every doSync(), but only if some page's hasChanged() reports a change in its saved state variables.


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. ctl.startApp() installs a window.onload handler that first awaits storage.migrateLegacyLocalStorage() (a failure there never prevents startup) and then initializes the Controller and processes the pages. See App Startup.

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 one to three elements: [from], [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 only used when to is given), 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. to may be omitted ([from]), in which case the matching key or keys are deleted from sVarState.

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.


deviceType

The device type passed to ctl.startApp().

Value

A string: 'Desktop', 'Phone' or 'Tablet'.


isActive

True when the app is in the foreground. It starts false and is only set when the first pause, resume, pageshow, pagehide or visibilitychange event arrives.

Value

A Boolean.


isInitialized

True once the Controller's initialization is complete.

Value

A Boolean.


isOnline

True when the device is connected to the Internet.

Value

A Boolean.


localStorageKey

The string ${appId}Input, set by ctl.startApp() from the app ID (the id option, or the title if id is omitted).

Value

A string.


sessionPages

The set of IDs of the pages visited during the current session. It is cleared when a new session starts and reported to the server in the session start marker.

Value

A Set of page ID strings.


Global ctl

The Controller is also available as window.ctl for debugging.