Interface sap.ui.core.util.XMLPreprocessor.IContextModule: sap/ui/core/util/XMLPreprocessor


Context interface provided by XML template processing as an additional first argument to any formatter function which opts in to this mechanism. Candidates for such formatter functions are all those used in binding expressions which are evaluated during XML template processing, including those used inside template instructions like <template:if>. The formatter function needs to be marked with a property requiresIContext = true to express that it requires this extended signature (compared to ordinary formatter functions). The usual arguments are provided after the first one (currently: the raw value from the model).

This interface provides callback functions to access the model and path which are needed to process OData V4 annotations. It initially offers a subset of methods from sap.ui.model.Context so that formatters might also be called with a context object for convenience, e.g. outside of XML template processing (see below for an exception to this rule).

Example: Suppose you have a formatter function called "foo" like below and it is used within an XML template like <template:if test="{path: '...', formatter: 'foo'}">. In this case foo is called with arguments oInterface, vRawValue such that oInterface.getModel().getObject(oInterface.getPath()) === vRawValue holds.

window.foo = function (oInterface, vRawValue) {
    //TODO ...
};
window.foo.requiresIContext = true;

Composite Binding Examples: Suppose you have the same formatter function and it is used in a composite binding like <Text text="{path: 'Label', formatter: 'foo'}: {path: 'Value', formatter: 'foo'}"/>. In this case oInterface.getPath() refers to ".../Label" in the 1st call and ".../Value" in the 2nd call. This means each formatter call knows which part of the composite binding it belongs to and behaves just as if it was an ordinary binding.

Suppose your formatter is not used within a part of the composite binding, but at the root of the composite binding in order to aggregate all parts like <Text text="{parts: [{path: 'Label'}, {path: 'Value'}], formatter: 'foo'}"/> . In this case oInterface.getPath(0) refers to ".../Label" and oInterface.getPath(1) refers to ".../Value". This means, the root formatter can access the ith part of the composite binding at will (since 1.31.0); see also getInterface. The function foo is called with arguments such that oInterface.getModel(i).getObject(oInterface.getPath(i)) === arguments[i + 1] holds.

To distinguish those two use cases, just check whether oInterface.getModel() === undefined, in which case the formatter is called on root level of a composite binding. To find out the number of parts, probe for the smallest non-negative integer where oInterface.getModel(i) === undefined. This additional functionality is, of course, not available from sap.ui.model.Context, i.e. such formatters MUST be called with an instance of this context interface.


Since: 1.27.1.
Method Summary
sap.ui.core.util.XMLPreprocessor.IContext.getInterface(iPart?, sPath?)Returns a context interface for the indicated part in case of the root formatter of a composite binding.
sap.ui.core.util.XMLPreprocessor.IContext.getModel(iPart?)Returns the model related to the current formatter call.
sap.ui.core.util.XMLPreprocessor.IContext.getPath(iPart?)Returns the absolute path related to the current formatter call.
sap.ui.core.util.XMLPreprocessor.IContext.getSetting(sName)Returns the value of the setting with the given name which was provided to the XML template processing.
Method Detail
sap.ui.core.util.XMLPreprocessor.IContext.getInterface(iPart?, sPath?): sap.ui.core.util.XMLPreprocessor.IContext
Returns a context interface for the indicated part in case of the root formatter of a composite binding. The new interface provides access to the original settings, but only to the model and path of the indicated part:
this.getInterface(i).getSetting(sName) === this.getSetting(sName);
this.getInterface(i).getModel() === this.getModel(i);
this.getInterface(i).getPath() === this.getPath(i);

If a path is given, the new interface points to the resolved path as follows:

this.getInterface(i, "foo/bar").getPath() === this.getPath(i) + "/foo/bar";
this.getInterface(i, "/absolute/path").getPath() === "/absolute/path";
A formatter which is not at the root level of a composite binding can also provide a path, but must not provide an index:
this.getInterface("foo/bar").getPath() === this.getPath() + "/foo/bar";
this.getInterface("/absolute/path").getPath() === "/absolute/path";
Note that at least one argument must be present.
Parameters:
{number}iPart? index of part in case of the root formatter of a composite binding
{string}sPath? a path, interpreted relative to this.getPath(iPart)
Since:
1.31.0
Exceptions:
{Error} in case an index is given but the current interface does not belong to the root formatter of a composite binding, or in case the given index is invalid (e.g. missing or out of range), or in case a path is missing because no index is given, or in case a path is given but the model cannot not create a binding context synchronously
Returns:
{sap.ui.core.util.XMLPreprocessor.IContext} the context interface related to the indicated part
sap.ui.core.util.XMLPreprocessor.IContext.getModel(iPart?): sap.ui.model.Model
Returns the model related to the current formatter call.
Parameters:
{number}iPart? index of part in case of the root formatter of a composite binding (since 1.31.0)
Returns:
{sap.ui.model.Model} the model related to the current formatter call, or (since 1.31.0) undefined in case of a root formatter if no iPart is given or if iPart is out of range
sap.ui.core.util.XMLPreprocessor.IContext.getPath(iPart?): string
Returns the absolute path related to the current formatter call.
Parameters:
{number}iPart? index of part in case of the root formatter of a composite binding (since 1.31.0)
Returns:
{string} the absolute path related to the current formatter call, or (since 1.31.0) undefined in case of a root formatter if no iPart is given or if iPart is out of range
sap.ui.core.util.XMLPreprocessor.IContext.getSetting(sName): any
Returns the value of the setting with the given name which was provided to the XML template processing.
Parameters:
{string}sName the name of the setting
Exceptions:
{Error} if the name is one of the reserved names: "bindingContexts", "models"
Returns:
{any} the value of the setting