JavaScript Delivery Context API (ai.dc)

To use Delivery Context Properties in client-side JavaScript, enable the API in conf/config.xml with:

<config>
  <js-api>
    <dc />
  </js-api>
</config>

API:

ai.dc.getProperty

ai.dc.getProperty(name, fallback)

Retrieve the value of a property stored in the Delivery Context, if defined. If a property with the given name is not defined in the Delivery Context, the given fallback value is returned; if no fallback value is provided, the return value is undefined (this may be especially important for custom properties set via the set-dc flow action).

  • @param {string} name the name of the property
  • @param {mixed} fallback a default value if the property with the given name is not set
  • @return {mixed} the stored value, the fallback value or undefined if not present

ai.dc.setProperty

ai.dc.setProperty(name, value)

Set the value of a property stored in the Delivery Context.

  • @param {string} name the name of the property
  • @param {mixed} value the value to set the property to
  • @return {Object} the Delivery Context API interface for chaining

Chaining setProperty calls:

ai.dc.setProperty('custom/foo1', 'bar').setProperty('custom/foo2', 'baz');

Note: Setting a property via the JavaScript Delivery Context API only changes the value on the client. It has no effect on the server side Delivery Context. Therefore the changed value will be reset after every normal or Partial Page Loading request.

ai.dc.listProperties

ai.dc.listProperties()

Get the list of available values

  • @return {array} a list of all property names that are stored

dcupdate Event:

Every update to the client side Delivery Context (e.g. because of a Partial Page Loading request or by using ai.dc.setProperty) will generate a dcupdate event.

ai.addEventListener('dcupdate', function(e) {
  alert('Changed Property: ' + e.property);
});

Event Data

The data object passed to the listener function has the following properties:

  • type
    • String "dcupdate", the type of the data object.
  • property
    • String, the name of the property that has changed.
  • oldValue
    • Mixed, the value the property had before it was changed.
  • newValue
    • Mixed, the value the property has after it was changed.