CustomEvent
A browser event type you create and name yourself, which carries any data you like in its detail property.
In more detail
new CustomEvent('cart-add', { detail: item, bubbles: true, cancelable: true }) is dispatched like any other event. Components can announce what happened without knowing who is listening.
Example
el.dispatchEvent(new CustomEvent('cart-add', { detail: { id: 'prod-99', qty: 2 }, bubbles: true }))Related terms
- EventTarget — The built-in browser interface for anything that can receive events, providing addEventListener, removeEventListener and dispatchEvent.
- dispatchEvent() — The method that sends an event object to a target so its listeners run, synchronously, before the call returns.
- event bus — A shared object that parts of an application use to publish events and subscribe to them, so senders and receivers never reference each other directly.
- cancelable option — An event option saying listeners may call preventDefault to cancel the action the event announces.
Taught in
- Passive Listeners, Scroll Performance And Touch Interactions — Frontend Architecture & Web Platform
- CustomEvent Dispatching And Decoupled Component Event Buses — Frontend Architecture & Web Platform