Set Triggers

A component can include triggers that will execute actions in other components. You must register triggers to be raised by components.

The component provides a payload for a trigger, which is passed to any action that is executed when the trigger is raised. You can select what actions will be executed for each trigger. Components that are built to work together can automatically raise triggers to execute actions without user interaction.

Register Triggers

For a custom component, triggers are registered as part of the registration data for the component. To add a trigger, update the "triggers" property array with each trigger the component supports. You also must specify the payload the trigger supports so that the user interface can be created to allow users to map values within the payload to properties supported by the action.

  1. Edit the appinfo.json file and review the "triggers":[], entry:
    "triggers": [{
        "triggerName": "helloWorldWhoAreYou",
        "triggerDescription": "Show Who I Am",
        "triggerPayload": [{
          "name": "whoAreYou",
          "displayName": "Who I Am"
       }]
    }],
  2. Sync the file to the sites server.

In this sample trigger entry, you’ve defined a triggerName ("helloWorldWhoAreYou"). The name value must be unique. You’ve then given the trigger a description ("Show Who I Am"), which is used by the user interface dialog to display your trigger. Finally, you’ve defined a single value payload for the trigger; users will be able to select entries in this payload and map them to fields in the action.

Once a trigger is registered, you should be able to see and select the trigger when you go to the Link tab in the Settings panel for your component.

Raise Triggers

Triggers can be raised at any point by a component. Typically, a trigger is raised by a user interaction, such as clicking a button or selecting a row in a table. A component can raise the trigger based on any criteria, for example, when data changes because of a REST API call. You can execute any number of actions when a trigger is raised.

Here’s an example of how to raise a trigger:
  1. Edit the render.js file and add a JavaScript function in the viewModel object that will call the Sites SDK to raise the trigger.
    self.raiseTrigger = function (triggerName) {
      SitesSDK.publish(SitesSDK.MESSAGE_TYPES.TRIGGER_ACTIONS, {
        'triggerName': 'helloWorldWhoAreYou', 
        'triggerPayload': { "whoAreYou": "This is " + self.whoAreYou() + "!"}
      });
    };
  2. Add an entry in the user interface to call the function to raise the trigger (-edit template.html) and a button before the </div>.
    <button data-bind="click raiseTrigger">Who Am I?</button>
  3. Sync or upload the render.js file to your Oracle Content Management instance server.

In the ViewModel object, you created a JavaScript function that is called when the button is clicked. This function calls the Sites SDK to tell it to trigger all the actions defined for this trigger "helloWorldWhoAreYou". It also passes through a triggerPayload that has a single field, "whoAreYou". These values "helloWorldWhoAreYou" and "whoAreYou" match those you entered when you registered the trigger in the previous step.

Note:

There is no predefined order to when an action is executed. Although each action will be called in the order it is listed, there is no wait for it to complete before the next action is called. If an action makes an asynchronous call, it may not complete before the next action is executed.