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("playerReady", 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 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.

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("playerReady", 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("playerReady", 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("playerReady");
                    

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

                        
storm.removeAllEventListeners();
                    
Next Step

For the next step please check our Storm JavaScript Library - Events where you’ll learn about available events for library behaviour.

Support Needed?

Create a free ticket and our support team will provide you necessary assistance.