Context Library

Contents

Context Library

This library contains functions to access and use the current context. It comes bundled with the XP runtime, so it should always use the same version as XP.

Usage

Add the following into your build.gradle file:

dependencies {
  include 'com.enonic.xp:lib-event:${xpVersion}
}

In your JavaScript controller, add a require statement:

const eventLib = require('/lib/xp/event');

You are now free to use the library functionality.

Functions

listener

Adds an event listener to the system

Parameters

An object with the following keys and their value:

Name Kind Details

type

string

Event type pattern

callback

function

Callback event listener

localOnly

boolean

Local events only (default to false)

Returns

null

Example

Adding a listener that logs the event
eventLib.listener({
    type: 'node.*',
    localOnly: false,
    callback: function (event) {
        log.info(JSON.stringify(event));
    }
});

send

Sends a custom event. All custom events are prefixed with "custom.".

Parameters

An object with the following keys and their value:

Name Kind Details

type

string

Event type

distributed

boolean

True if it should be distributed in cluster

data

object

Additional data for event.

Returns

null

Example

Send a custom event into the system
eventLib.send({
    type: 'myEvent',
    distributed: false,
    data: {
        a: 1,
        b: 2
    }
});

Contents