Context

The object type that modules use to interact with the environment. Used exclusively within Box.Application, but exposed publicly for easy testing.

getElement

Description

Returns the element that represents the module.

Returns

An HTMLElement object.

Example

Box.Application.addModule('abc', function(context) {
    return {
        init: function() {
            // outputs HTMLElement with id 'mod-test-module'
            console.log(context.getElement());
        }
    };
});

getConfig

Description

Retrieves a module’s configuration data from embedded JSON in a ‘text/x-config’ script tag. This method is a proxy to Box.Application.getModuleConfig but with a shorter name.

Usage

Parameter Type Description
name string Specific configuration value to retrieve.

Example

<div id="mod-test-module" data-module="test-module">
    <script type="text/x-config">{"foo": "bar"}</script>
    ...
</div>
Box.Application.addModule('abc', function(context) {
    return {
        init: function() {
            // Outputs "bar"
            console.log(context.getConfig('foo'));
        }
    };
});

Proxy Methods


broadcast

Description

Broadcasts a message to all registered listeners. A proxy to Box.Application.broadcast

Usage

Parameter Type Description
name string Name of the message
data any Custom parameters for the message

Example

<div id="mod-search-bar" data-module="search-bar">
    ...
</div>
Box.Application.addModule('search-bar', function(context) {
    return {
        search: function() {
            context.broadcast('searchcomplete', {
                numResults: 100
            });
        }
    };
});

getService

Description

Retrieves an instance of a registered service. A proxy to Box.Application.getService

Usage

Parameter Type Description
service string Name of service.

Returns

T3 Service or null.

Example

Box.Application.addModule('abc', function(context) {
    var dom;

    return {
        init: function() {
            dom = context.getService('dom');
        }
    };
});

getGlobal

Description

Returns a global variable. This function exists to make accessing globals more explicit.

Usage

Parameter Type Description
name string Name of global.

Returns

The global variable if it exists or null.

Example

var navigator = context.getGlobal('navigator');
console.log(navigator.userAgent);

getGlobalConfig

Description

Retrieves a configuration value that was passed through init. A proxy to Box.Application.getGlobalConfig

Usage

Parameter Type Description
key string Config key.

Returns

Object.

Example

Box.Application.init({
    username: 'bob'
});

Box.Application.addModule('abc', function(context) {
    return {
        init: function() {
            // Outputs "bob"
            console.log(context.getGlobalConfig('username'));
        }
    };
});