You can add custom events to the components dynamically by using the addEventHandler() method in the component object.
The syntax for a custom event is as follows:
addEventHandler(String event, Function handler, String phase, String includeFacets)
The following provides a description for each of the parameters of the addEventHandler component method:
- The first argument is the name of the event that triggers the handler. The <aura:registerEvent> tag in a component's markup declares the event that the component fires.
For a component event, set this argument to match the name attribute of the <aura:registerEvent> tag. For an application event, set this argument to match the event descriptor in the format namespace:eventName.
- The second argument is the action that handles the event. The format is similar to the value that you would put in the action attribute in the <aura:handler> tag, if the handler was statically defined in the markup. There are two options for this argument.
- To use a controller action in the handler, use the following format:
cmp.getReference("c.actionName")
- To use an anonymous function, use the following format:
function(auraEvent) {
// handling logic here
}