Events & Listeners Basics - Storm JavaScript Library

Learn how to use event-listener system inbuilt into Storm Library for direct communication with your code.

Attaching an event listener

The concept of attaching events is the same as in standard JavaScript.

                        
const storm = stormLibrary(config);

storm.addEventListener("playerCoreReady", onPlayerReady);
                    
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 playerCoreReady) to the library. Whenever the library (player) is ready, an attached function will be called. The function can also be of an inline type.

Now we need to define our function.

                        
function onPlayerReady(event) {
    console.log(event.ref); // reference to the library object
};
                    

We can also use inline functions.


storm.addEventListener("playerCoreReady", function(event){
      console.log(event.ref); // reference to the library object
});
                    

Detaching an event listener

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

                        
storm.removeEventListener("playerCoreReady", onPlayerReady);
                            

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

                        
storm.removeEventListener("playerCoreReady");
                    

We can also remove all event listeners from our library element:

                        
storm.removeAllEventListeners();