Triggers

In some cases, after running one action on the component, you’d like to execute another action on the same or different component immediately, just like a trigger in database. For example, after a new member has registered, which corresponds to the insert action on the member table, you’d like to run another insert on table sponsor, to credit who is the reference of the new member.

Trigger is just an easy way to chain existing actions of different components together. Those actions can be either RESTful ones or the ones you have developed.

To use trigger, define the following nextpages in component.json:

{
  ...
  "nextpages" : {
    "insert" : [ 
      {
        "model"  : "Sponsor",
        "action" : "insert", 
        "relate_item" : {"member_id":"member_id", "sponsor_id":"sponsor_id"},
        "manual" : {"credits" : 30 }
      },
      { ... }
    ],
    "topics" : [
    ]
  }
  ...
}

So to trigger an action, create an object with keys

  • model: the name of the other Model
  • action: the action on the other Model
  • relate_item: defining a column mapping between the two tables. Their values will be passes correspondingly.
  • manual: you can manually pass some variables.

As you could see, one action could trigger multiple Models.

In Genelet, the main data resulted is an array. If relate_item exists, Genelet will scan all the items in the array, and run one trigger for each item. The data returned from the trigger will be saved in the new key named by model (e.g. “Sponsor”) in the item. In the Model class, this is achieved in method call_next.

If relate_item does not exists, the other model will take only the original query variables as inputs. Then the resultant data are assigned to the key named by model (e.g. “Sponsor”) in OTHER, which is further delivered to the client in “relationships”. In the Model class, this is achieved in method call_once. Please consult each language manual for specific implementations of call_next, call_once and OTHER.