Built-in context functions.
Example
var contextLib = require('/lib/xp/context');
Methods
(static) get() → {object}
Returns the current context.
Returns:
Return the current context as JSON object.
- Type
- object
Examples
// Returns the current context.
var result = contextLib.get();
log.info('Context as JSON %s', result);
// Context returned.
var expected = {
"branch": "draft",
"repository": "cms-repo",
"authInfo": {
"principals": [
"user:system:anonymous",
"role:system.everyone"
]
}
};
(static) run(context, callback) → {object}
Runs a function within a specified context.
Parameters:
Name | Type | Description | ||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
context |
object | JSON parameters. Properties
|
||||||||||||||||||||||||
callback |
function | Function to execute. |
Returns:
Result of the function execution.
- Type
- object
Example
// Define the callback to be executed.
function callback() {
return 'Hello from context';
}
// Executes a function using different context.
var result = contextLib.run({
branch: 'draft',
user: {
login: 'su',
userStore: 'system'
}
}, callback);
log.info('Callback says "%s"', result);