Attaching and detaching events - Storm JavaScript Library

Storm library utilizes a simple event-listener model for direct communication. While actions can be executed using available methods (API), a feedback from player is also necessary.

Attaching an event listener

Attaching an event lister is a straightforward task

                        
const storm = stormLibrary(config);

storm.addEventListener("playerReady", function(event){
    console.log("onPlayerReady",event);
})
                    
The API for all JavaScript bundle formats (IIFE, UMD, AMD etc.) is the same. The example above uses IIFE format.

In this example, we have added a new event listener (called playerReady) to the library. Whenever the library (player) is ready, an attached function will be called. The function can also be of an inline type.

                        
storm.addEventListener("playerReady", onPlayerReady);
                    

A more advanced variation is also possible (and suggested):

                        
storm.addEventListener("playerReady", onPlayerReady, thisReference, 10);
                    

The thisReference object allows you to pass a reference for this element within the onPlayerReady function, while eventData contains data related to the player. The last parameter indicates listener priority (0 is default).


function onPlayerReady(thisReference, eventData){

    console.log(eventData.getInfo().getDispatcher()) // player
    console.log(eventData.getInfo().getEventName()) // playerReady
    console.log(eventData.getInfo().getData()) // additional data related to the event

}
                    

Detaching an event listener

In order to detach (remove) an event listener just use:

                        
storm.removeEventListener("playerReady", onPlayerReady);
                            

For obvious reasons, inline functions cannot be removed (unless stored as a variable), however, there is a way to remove all events from the player:

                        
storm.removeAllEventListeners();