π 2/8 JS Reactive Patterns: Custom Event Targets
#js #patterns #customEvent
#js #patterns #customEvent
If you prefer not to dispatch events globally on the window object, you can create your own event target.
By extending the native EventTarget class, you can dispatch events to a new instance of it. This ensures that your events are triggered only on the new class itself, avoiding global propagation. Moreover, you have the flexibility to attach handlers directly to this specific instance.
π4
π 7/8 JS Reactive Patterns: Custom Events as a Browser Version Of PubSub
#js #patterns #CustomEvent
#js #patterns #CustomEvent
The browser offers an API for triggering and subscribing to custom events through the CustomEvent class and the dispatchEvent method. The latter provides us with the ability not only to trigger an event but also to attach any desired data to it.