3.7.1Handling context - synchronization in the page container

This section is only applicable to widgets. A script or UDIX cannot (directly) be part of the MyTrader page container.

If your widget offers a selection of market or timeframe, you may want to respect "context" where the user can choose to synchronize the markets and/or timeframes on a page. Doing this consists of three parts:

Reading any context when your widget starts up

Handling changes of context while your widget is running

Sending changes of context to the page container if the user changes the market or timeframe selection from inside your widget

Any existing context on start-up is provided by the Framework.context property. If present, this may contain one or both of an instrumentId and a timeframe. The typical way of handling this is to inspect it and, if present, to use it to override any private or category settings which the widget has.

The example above about widget settings has the following code:

... code which examines private and category settings, and builds Framework.$myState …

// And, finally, do UI initialisation based on the settings or defaults

Framework.$UpdateMyUI();

A typical way of handling context would be, as a final stage after loading stored settings or using defaults, to overwrite any market or timeframe selection if the page container is providing context. For example:

... code which examines private and category settings, and builds Framework.$myState …

// The addition:

// If the page container has instrument context, overwrite whatever settings we have built up

if (Framework.context && Framework.context.instrumentId) {

Framework.$myState.instrumentId = Framework.context.instrumentId;

}

// And, finally, do UI initialisation based on the settings or defaults

Framework.$UpdateMyUI();

There may also be changes in context while your widget is running, as a result of the user changing the market and/or timeframe selection in another widget. You receive these changes as a WIDGET_CONTEXT_CHANGE message, and you would typically handle it the same way as the user changing the selection within your own widget.

Finally, if the user changes the market and/or timeframe selection within your own widget, using your own UI, you broadcast that change to any other widgets in the same page as yours. You do this using Framework.ReportContext(). For example:

Framework.ReportContext({

instrumentId: <new market selection>,

timeframe: <new timeframe selection>

});

Note: calling ReportContext() also causes all other widgets and scripts to receive a WIDGET_INSTRUMENT_SELECTION message, notifying them of the change.