Skip to main content
Version: 11.2

$.udb.executeInContext()

note

This article is about the executeInContext() function of the $.udb object.

$.udb.executeInContext()

Switches the default context of a function call. Use this function if there is a need to cross the boundaries of context, so that you need to explicitly identify the target context. This occurs, for example, when a VariableSet data source on a page in one frame must be accessed by controls on a page in a different frame.

Returns the result of executing the func function. This could be a result value, or a UdbPromise object that yields a result value.

While func is executing, $.udb.currentFrameId is set to context; it is automatically restored to its previous value once func finishes.

Syntax

$.udb.executeInContext( *context*, *func* )

The optional context refers to the root context if it has the value '' (empty string). A subframe within the root context is referred to by the name of the frame. In context, names of frames contained in frames are separated from the name of the containing frame by a forward slash:

$.udb.executeInContext('MyFrame/SubFrame', function() {
//...
});

If omitted, the function returns the current context instead, which is the root context if no other context is currently being used.

The required func is the function call for which the default context is being switched.

Using .executeInContext() is largely automated, because if you perform a .then() call on an object that already has a context (for example a data source), then the .then() clause of that object automatically inherits that context as well. Only if changing the context is explicitly necessary or when no context is known (e.g. in a setTimeout() function), the use of .executeInContext() may be needed.

As an illustration:

$.udb('TOUR').executeQuery().then((dsc) => {
var searchCols = {};
dsc.searchCols().each((index,col) => {
searchCols[col.name()] = col.val();
});

//use the context of dsc for navigation:
$.udb.executeInContext( dsc.context(), () => {
$.udb.navigateTo('Tours Info 1.2 Results')
.then(() => {
$.udb('TOUR').searchCols().forEach((index,col) => {
col.val(searchCols[col.name()]);
});
});
});
});

In the example, the .executeInContext() call is not necessary:

$.udb('TOUR').executeQuery().then((dsc) => {
var searchCols = {};
dsc.searchCols().each((index,col) => {
searchCols[col.name()] = col.val();
});

//context is inherited from the table, so we do not need to set it:
$.udb.navigateTo('Tours Info 1.2 Results')
.then(() => {
$.udb('TOUR').searchCols().forEach((index,col) => {
col.val(searchCols[col.name()]);
});
});
});