Dynamically Registering an Event Handler to an Event

Components you use within another component can have events. An event of an interface definition is triggered in the component in which the interface definition is implemented.

An event handler of the using component can register onto such an event. If the usage of a component has been created dynamically, then also the registration of the using component onto an event of the used component or the implemented interface must be created dynamically. The code fragment below shows an example of how to implement a dynamic registration.

method MY_CONTROLLER_METHOD .
data: L_COMPONENT_API           type ref to IF_WD_COMPONENT,
L_COMPONENT_USAGE         type ref to IF_WD_COMPONENT_USAGE.
L_COMPONENT_API = WD_COMP_CONTROLLER->WD_GET_API( ).
L_COMPONENT_USAGE->ADD_EVENT_HANDLER(
listener                    = L_COMPONENT_API
handler_name                = <event_handler_name>
controller_name             = 'INTERFACECONTROLLER'
event_name                  = <event_name>     ).
endmethod.

Method ADD_EVENT_HANDLER ( ) is called at the object of the component usage L_COMPONENT_USAGE. The controller_name attribute describes the controller in which the event is defined:  If, as described in this case, the event is defined in a used component, this can only be its interface controller.