Skip to content

Event Handler Asset

Event handlers triggered by the APIs for data, messaging, file and geolocation services must declare an "asset". Asset is a general term used to identify one of the following:

  • a data table for the Data service APIs.
  • a messaging channel for the Messaging APIs.
  • a file or a directory for the File service APIs.
  • a geocategory for the Geolocation APIs.

Consider the following example, which is an event handler for the Data Service's beforeUpdate API event:

/**
* @param {Object} req The request object contains information about the request
* @param {Object} req.context The execution context contains an information 
*                             about application, current user and event
* @param {Order} req.item An item with changes
*
* @returns {Order|Promise.<Order>|void} By returning a value you can stop 
*                                          further event propagation and return
*                                          a specific result to the caller
*/
Backendless.ServerCode.Persistence.beforeUpdate('Order', function(req) {
  //add your code here
});
Notice the first argument for the beforeUpdate function, it is the name of the table for which the event handler is registered.. When event handlers are created in Backendless Console, it automatically selects the appropriate asset (table, messaging channel, file/directory, geo category) and puts it into the generated code.

It is possible to create a generic event handler, which would respond to all API events for a given service. For example, the event handler below is triggered when a new object is created in any of the application's tables:

/**
* @param {Object} req The request object contains information about the request
* @param {Object} req.context The execution context contains an information about 
*                             application, current user and event
* @param {Object} req.item An item to create
*
* @returns {Object|Promise.<Object>|void} By returning a value you can stop 
*                                            further event propagation and return
*                                            a specific result to the caller
*/
Backendless.ServerCode.Persistence.beforeCreate('*', function(req) {
  //add your code here
});
Notice the first argument of the beforeCreate function is "*".  This indicates that the event handler will be triggered for the beforeCreate events for all tables in the application.