Skip to main content
Pentaho Documentation

Context

pentaho.type.Context

A class that holds configured types.

When a component, like a visualization, is being assembled, it should not necessarily be unaware of the environment where it is going to be used. A context object gathers information that has a global scope, such as the current locale or the current theme, which is likely to have an impact on how a visualization is presented to the user. For instance, the color palette used in a categorical bar chart might be related to the current theme. As such, besides holding contextual, environmental information, a context object should contain the necessary logic to facilitate the configuration of component types using that information. The Pentaho Type API embraces this concept by defining types as type factories that take a context object as their argument.

The instance constructors of types must be obtained from a context object, using one of the provided methods: get, getAsync, getAll, or getAllAsync, or inject, so that these are configured before being used. This applies whether an instance constructor is used for creating an instance or to derive a subtype.

A type context holds environmental information in the form of an environment of the JavaScript Pentaho Platform, which contains relevant information such as: application, user, theme and locale. Their values determine (or "select") the type configuration rules that apply and are used to configure the constructors provided by the context.

To better understand how a context provides configured types, assume that an non-anonymous type, with the id "my/own/type", is requested from a context object, context:

var MyOwnInstanceCtor = context.get("my/own/type");

Internally, (it is as if) the following steps are taken:

  1. If the requested type has been previously created and configured, just return it:

    var InstanceCtor = getStored(context, "my/own/type");
    if(InstanceCtor != null) {
      return InstanceCtor;
    }
  2. Otherwise, the context requires the type's module from the AMD module system, and obtains its factory function back:

    var typeFactory = require("my/own/type");
  3. The factory function is called with the context as argument and creates and returns an instance constructor for that context:

    InstanceCtor = typeFactory(context);
  4. The instance constructor is configured with any applicable rules:

    InstanceCtor = configure(context, InstanceCtor);
  5. The configured instance constructor is stored under its identifier:

    store(context, InstanceCtor.type.id, InstanceCtor);
  6. Finally, it is returned to the caller:

    return InstanceCtor;

Note that anonymous types cannot be directly configured, as type configuration rules are targeted at specific, identified types.

AMD Module

require(["pentaho/type/Context"], function(Context) { /* code goes here */ });

Constructor

Name Description
new Context(platformContextSpec)

Creates a Context with given variables.

Members

Name Description
instance : pentaho.type.Context
 
Static

Gets the default type context of the Pentaho Type API.

_byFactoryUid : Object.<string, Class.<pentaho.type.Instance>>

Map of instance constructors by factory function uid.

_byTypeId : Object.<string, Class.<pentaho.type.Instance>>

Map of instance constructors by type id and by type alias for non-anonymous types.

_byTypeUid : Object.<string, Class.<pentaho.type.Instance>>

Map of instance constructors by type uid.

_Instance : Class.<pentaho.type.Instance>

The root Instance constructor.

transaction : pentaho.type.changes.Transaction

Gets the ambient transaction, if any, or null.

vars : pentaho.context.IContext

Gets the associated platform context.

Methods

Name Description
enterChange() : pentaho.type.changes.TransactionScope

Enters a scope of change.

enterCommitted() : pentaho.type.changes.CommittedScope

Enters a read-committed scope.

get(typeRef, keyArgs) : Class.<pentaho.type.Instance>

Gets the configured instance constructor of a type.

getAll(baseTypeId, keyArgs) : Array.<Class.<pentaho.type.Value>>

Gets the configured instance constructors of all of the loaded types that are subtypes of a given base type.

getAllAsync(baseTypeId, keyArgs) : Promise.<Array.<Class.<pentaho.type.Instance>>>

Gets a promise for the configured instance constructors of all of the types that are subtypes of a given base type.

getAsync(typeRef, keyArgs) : Promise.<!Class.<pentaho.type.Instance>>

Gets, asynchronously, the configured instance constructor of a type.

inject(typeRefs, fun, ctx) : function

Binds the initial arguments of a function to the instance constructors corresponding to given type references.

Constructor Details

new Context(platformContextSpec)

Creates a Context with given variables.

Source: javascript/web/pentaho/type/Context.js, line 154

Parameters:
Name Default Value Summary
platformContextSpec : pentaho.context.spec.IContext
 
Optional

The context variables' specification. When unspecified, it defaults to pentaho.context.main. Unspecified platform context properties default to the value of those of the default context.

Members Details

instance: pentaho.type.Context
 
Static

Gets the default type context of the Pentaho Type API.

This type context instance is created with the Pentaho Platform's default context variables, as given by pentaho.context.main.

Source: javascript/web/pentaho/type/Context.js, line 1161

_byFactoryUid: Object.<string, Class.<pentaho.type.Instance>>

Map of instance constructors by factory function uid.

See also _nextFactoryUid and getFactoryUid.

Source: javascript/web/pentaho/type/Context.js, line 197

_byTypeId: Object.<string, Class.<pentaho.type.Instance>>

Map of instance constructors by type id and by type alias for non-anonymous types.

Source: javascript/web/pentaho/type/Context.js, line 214

_byTypeUid: Object.<string, Class.<pentaho.type.Instance>>

Map of instance constructors by type uid.

Source: javascript/web/pentaho/type/Context.js, line 204

_Instance: Class.<pentaho.type.Instance>

The root Instance constructor.

Source: javascript/web/pentaho/type/Context.js, line 221

transaction: pentaho.type.changes.Transaction

Gets the ambient transaction, if any, or null.

Source: javascript/web/pentaho/type/Context.js, line 625

vars: pentaho.context.IContext

Gets the associated platform context.

Source: javascript/web/pentaho/type/Context.js, line 242

Methods Details

enterChange() : pentaho.type.changes.TransactionScope

Enters a scope of change.

To mark the changes in the scope as error, call its reject method.

To end the scope of change successfully, dispose the returned transaction scope, by calling its dispose method.

If the scope initiated a transaction, then that transaction is committed. Otherwise, if an ambient transaction already existed when the change scope was created, that transaction is left uncommitted.

To end the scope with an error, call its reject method.

Source: javascript/web/pentaho/type/Context.js, line 650

Returns:
Name Description
pentaho.type.changes.TransactionScope

The new transaction scope.

enterCommitted() : pentaho.type.changes.CommittedScope

Enters a read-committed scope.

Within this scope there is no current transaction and reading the properties of instances obtains their committed values.

Source: javascript/web/pentaho/type/Context.js, line 663

Returns:
Name Description
pentaho.type.changes.CommittedScope

The read-committed scope.

get(typeRef, keyArgs) : Class.<pentaho.type.Instance>

Gets the configured instance constructor of a type.

For more information on the typeRef argument, see UTypeReference.

The modules of standard types and refinement facet mixins are preloaded and can be requested synchronously. These are:

For all of these, the pentaho/type/ or pentaho/type/facets/ prefix is optional (when requested to a context; the AMD module system requires the full module identifiers to be specified).

The filter types are also preloaded:

If it is not known whether all non-standard types that are referenced by identifier have already been loaded, the asynchronous method version, getAsync, should be used instead.

Source: javascript/web/pentaho/type/Context.js, line 352

Parameters:
Name Default Value Summary
typeRef : pentaho.type.spec.UTypeReference

A type reference.

keyArgs : Object
 
Optional

The keyword arguments.

Parameters:
Name Default Value Summary
defaultBase : pentaho.type.spec.UTypeReference
 
Optional

The default base type of typeRef when it is an immediate generic object specification.

Returns:
Name Description
Class.<pentaho.type.Instance>

The instance constructor.

Throws:
Name Description
pentaho.lang.ArgumentRequiredError

When typeRef is an empty string or Nully.

pentaho.lang.ArgumentInvalidError

When typeRef is of an unsupported JavaScript type: not a string, function, array or object.

pentaho.lang.ArgumentInvalidError

When typeRef is a type constructor (e.g. Type)

pentaho.lang.ArgumentInvalidError

When typeRef is an instance.

Error

When the identifier of a type is not defined as a module in the AMD module system (specified directly in typeRef, or present in an generic type specification).

Error

When the identifier of a non-standard type is from a module that the AMD module system has not loaded yet (specified directly in typeRef, or present in an generic type specification).

pentaho.lang.OperationInvalidError

When the value returned by a factory function is not an instance constructor of a subtype of Instance (specified directly in typeRef, or obtained indirectly by loading a type's module given its identiifer).

pentaho.lang.ArgumentInvalidError

When typeRef is, or contains, an array-shorthand, list type specification that has more than one child element type specification.

See also:  pentaho.type.Context#getAsync

Example

Getting a configured type instance constructor synchronously for a specific application.

require(["pentaho/type/Context", "my/viz/chord"], function(Context) {

 var context = new Context({application: "data-explorer-101"})

 // Request synchronously cause it was already loaded in the above `require`
 var VizChordModel = context.get("my/viz/chord");

 var model = new VizChordModel({outerRadius: 200});

 // Render the model using the default view
 model.type.defaultViewClass.then(function(View) {
 var view = new View(document.getElementById("container"), model);

 // ...
 });

});
getAll(baseTypeId, keyArgs) : Array.<Class.<pentaho.type.Value>>

Gets the configured instance constructors of all of the loaded types that are subtypes of a given base type.

This method is a synchronous version of pentaho.type.Context#getAllAsync

If it is not known whether all known subtypes of baseTypeId have already been loaded (for example, by a previous call to getAllAsync), the asynchronous method version, getAllAsync, should be used instead.

Source: javascript/web/pentaho/type/Context.js, line 506

Parameters:
Name Default Value Summary
baseTypeId : string
 
Optional

The identifier of the base type. It defaults to "pentaho/type/value".

keyArgs : object
 
Optional

Keyword arguments.

Parameters:
Name Default Value Summary
isBrowsable : boolean
 
Optional
null

Indicates that only types with the specified isBrowsable value are returned.

Returns:
Name Description
Array.<Class.<pentaho.type.Value>>

An array of instance contructors.

Throws:
Name Description
Error

When the identifier of a type is not defined as a module in the AMD module system.

Error

When the identifier of a non-standard type is from a module that the AMD module system has not loaded yet.

See also:  pentaho.type.Context#getAllAsync , pentaho.type.Context#get , pentaho.type.Context#getAsync

Example

Getting all "my/component" sub-types browsable in the application "data-explorer-101".

require(["pentaho/type/Context"], function(Context) {

 var context = new Context({application: "data-explorer-101"});

 var ComponentModels = context.getAll("my/component", {isBrowsable: true});
 ComponentModels.forEach(function(ComponentModel) {

 console.log("will display menu entry for: " + ComponentModel.type.label);

 });

});
getAllAsync(baseTypeId, keyArgs) : Promise.<Array.<Class.<pentaho.type.Instance>>>

Gets a promise for the configured instance constructors of all of the types that are subtypes of a given base type.

Any errors that may occur will result in a rejected promise.

Source: javascript/web/pentaho/type/Context.js, line 572

Parameters:
Name Default Value Summary
baseTypeId : string
 
Optional

The identifier of the base type. Defaults to "pentaho/type/value".

keyArgs : object
 
Optional

Keyword arguments.

Parameters:
Name Default Value Summary
isBrowsable : boolean
 
Optional
null

Indicates that only types with the specified isBrowsable value are returned.

Returns:
Name Description
Promise.<Array.<Class.<pentaho.type.Instance>>>

A promise for an array of instance classes.

See also:  pentaho.type.Context#get , pentaho.type.Context#getAsync

Example

Getting all "my/component" sub-types browsable in the application "data-explorer-101".

require(["pentaho/type/Context"], function(Context) {

 var context = new Context({application: "data-explorer-101"});

 context.getAllAsync("my/component", {isBrowsable: true})
 .then(function(ComponentModels) {

 ComponentModels.forEach(function(ComponentModel) {

 console.log("will display menu entry for: " + ComponentModel.type.label);

 });
 });

});
getAsync(typeRef, keyArgs) : Promise.<!Class.<pentaho.type.Instance>>

Gets, asynchronously, the configured instance constructor of a type.

For more information on the typeRef argument, see UTypeReference.

This method can be used even if a generic type specification references non-standard types whose modules have not yet been loaded by the AMD module system.

Source: javascript/web/pentaho/type/Context.js, line 452

Parameters:
Name Default Value Summary
typeRef : pentaho.type.spec.UTypeReference

A type reference.

keyArgs : Object
 
Optional

The keyword arguments.

Parameters:
Name Default Value Summary
defaultBase : pentaho.type.spec.UTypeReference
 
Optional

The default base type of typeRef when it is an immediate generic object specification.

Returns:
Name Description
Promise.<!Class.<pentaho.type.Instance>>

A promise for the instance constructor.

See also:  pentaho.type.Context#get

Example

Getting a configured type instance constructor asynchronously for a specific application.

require(["pentaho/type/Context"], function(Context) {

 var context = new Context({application: "data-explorer-101"})

 context.getAsync("my/viz/chord")
 .then(function(VizChordModel) {

 var model = new VizChordModel({outerRadius: 200});

 // Render the model using the default view
 model.type.defaultViewClass.then(function(View) {
 var view = new View(document.getElementById("container"), model);

 // ...
 });
 });

});
inject(typeRefs, fun, ctx) : function

Binds the initial arguments of a function to the instance constructors corresponding to given type references.

The specified type references are each resolved synchronously, using get, when the bound function is first called. Thus, any resolve errors are only thrown then.

If a fixed JavaScript context object is specified in ctx, then fun gets bound to that object. Otherwise, the JavaScript context object in which fun is called is dynamic (whichever the caller decides).

Source: javascript/web/pentaho/type/Context.js, line 376

Parameters:
Name Default Value Summary
typeRefs : Array.<pentaho.type.spec.UTypeReference>

An array of type references.

fun : function

The function to bind.

ctx : Object
 
Optional

The fixed JavaScript context object to use.

Returns:
Name Description
function

The new bound function.