Class: Core/Context

Core/Context


new Core/Context()

The Context object is a state variable to hold the current application state. It extends Backbone.Model.

The context contains various states of the current View or Layout -- this includes the current model and collection, as well as the current module focused and also possibly the url hash that was matched.

Instantiate a context.

const Context = require('core/context');
let myContext = new Context();

Creating a child context or retrieving an existing one.

Assuming myContext is a context instance.

let childContext = myContext.getChildContext({cid: <contextId>});

If does not match any child context, a new child context will be created.

Retrieving Data from the Context

A context is a Backbone Model, so its data is stored in the attributes:

var module = myContext.get('module'); // module = "Contacts"

Methods


addFields(fieldsArray)

Sets the fields attribute on this context by extending the current fields attribute with the passed-in fieldsArray.

Parameters:
Name Type Description
fieldsArray Array.<string>

The list of field names.

Returns:

Instance of this model.

Type
Core/Context

clear( [options])

Recursively clears this context and its children.

Unbinds the event listeners, clears the attributes, aborts the request in progress if any, and resets the load flag (calls Core/Context#resetLoadFlag).

Parameters:
Name Type Argument Description
options Object <optional>

Standard Backbone.Model options.


getChildContext( [def])

Gets an existing related context or creates a new one.

Parameters:
Name Type Argument Description
def Object <optional>

Child context definition.

Properties
Name Type Argument Description
cid string <optional>

The id of the context to retrieve. This takes precedence over the passed name, the link and the module.

name string <optional>

The name of the context to retrieve. This takes precedence over passed the link and the module.

link string <optional>

The link name to retrieve a context from. This takes precedence over the passed module name.

module string <optional>

The module name to retrieve a context from. The first child context matching the module attribute will be returned.

forceNew boolean <optional>

true to force the creation a new context, without trying to retrieve an existing one.

Returns:

The child context.

Type
Core/Context

initialize( [attributes])

Calls Backbone's initialize and initializes this context's properties.

Parameters:
Name Type Argument Description
attributes Object <optional>

The initial hash of attributes.


isCreate()

Checks if this context is used for a create view.

Returns:

true if this context has the create flag set.

Type
boolean

isDataFetched()

Checks if data has been successfully loaded.

Returns:

true if data has been fetched, false otherwise.

Type
boolean

loadData( [options])

Loads data (calls fetch on either model or collection).

This method sets an internal boolean flag to prevent consecutive fetch operations. Call Core/Context#resetLoadFlag to reset the context's state.

Parameters:
Name Type Argument Description
options Object <optional>

A hash of options passed to collection/model's fetch method.

Properties
Name Type Argument Description
fetch boolean <optional>

true to always fetch the data.


prepare( [force], prepareRelated)

Prepares instances of model and collection and sets them to this context.

The method also create related contexts, based on the created bean fields: It creates child context for each link of each collection field present on the bean. This method does nothing if this context already contains an instance of a model or a collection.

Parameters:
Name Type Argument Description
force boolean <optional>

true to force the creation of the model and collection.

prepareRelated boolean

true to always prepare related contexts.

Returns:

Returns this context instance.

Type
Core/Context

reloadData( [options])

Refreshes the context's data and refetches the new data if the skipFetch attribute is true.

Parameters:
Name Type Argument Description
options Object <optional>

Options for Core/Context#loadData and the reload event.

Fires:
  • event:reload

resetLoadFlag( [options])

Resets load-data state for this context and its child contexts.

The Core/Context#loadData method sets an internal boolean flag to prevent multiple identical requests to the server. This method resets this flag.

Parameters:
Name Type Argument Description
options Object <optional>

A hash of options.

Properties
Name Type Argument Default Description
recursive boolean <optional>
true

true to reset the child contexts too.

resetModel boolean <optional>
true

true to reset the flag on the model.

resetCollection boolean <optional>
true

true to reset the flag on the collection.


setFetch(fetch [, options])

Sets the fetch attribute recursively on the context and its children.

A context with fetch set to false won't load the data.

Parameters:
Name Type Argument Description
fetch boolean

true to recursively set fetch to true in this context and its children.

options Object <optional>

A hash of options.

Properties
Name Type Argument Description
recursive boolean <optional>

true to recursively set the fetch boolean on the children.

Events


reload

Triggered before and after the context is reloaded.

Parameters:
Name Type Argument Description
this Core/Context

The context instance where the event is triggered.

options Object <optional>

The options passed during Core/Context#reloadData call.