Description: $dispatch() enables decoupled communication in AlpineJS. Components can emit custom events, and other components (whether parents, siblings, or unrelated elements listened to via global listeners like window or document) can react to these events. This promotes modularity and reduces direct dependencies between components.
$dispatch('eventName', detailObject)
'eventName': A string representing the name of your custom event (e.g., 'item-selected', 'form-submitted'). It's good practice to use kebab-case or a similar convention.detailObject: An optional JavaScript object. This is the payload or data you want to send with the event. Listeners will access this data via $event.detail. If no data needs to be sent, this argument can be omitted.this.$dispatch('user-updated', { userId: 123, newEmail: 'test@example.com' }).@event-name.modifier="handler($event.detail)"
x-on:event-name.modifier) for listening to DOM events on an element.event-name: The name of the event to listen for. For custom events, this must match the 'eventName' used in $dispatch() (e.g., @user-updated)..modifier (optional): Modifiers can change how the event listener behaves. Important ones for this skill include:
.window: Attaches the listener to the global window object..document: Attaches the listener to the global document object..once, .prevent, .stop, .self, .outside.handler($event.detail): The JavaScript expression or component method to execute when the event is caught.
$event: Represents the native browser Event object.
$event.detail: This is where the detailObject (payload) sent with $dispatch() is found. For the example above, $event.detail would be { userId: 123, newEmail: 'test@example.com' }..window modifier
@my-custom-event.window="handleEventGlobally($event.detail)".window object instead of the component's root element or the element where the directive is placed.$dispatch will bubble. If a listener is on the window, it can catch any such event that reaches the window object.Using the .window modifier is crucial for global events or when the listening component is not a direct ancestor of the dispatching component.
.window (or .document), an event listener like @custom-event="handler" only listens for events bubbling up from children of the element it's on, or events dispatched directly on that element. It won't catch events from sibling components or unrelated parts of the DOM.window object is the ultimate common ancestor, making @custom-event.window="handler" a reliable way for Component B to listen globally.Event detail ($event.detail) is where the payload sent with $dispatch is found.
$dispatch('my-event', { message: 'Hello!' }), the data object { message: 'Hello!' } is not directly available as, for example, $event.message.CustomEvent interface (which Alpine uses under the hood) standardizes that custom data is passed via the detail property of the event object.$event.detail. So, to get the message, you would use $event.detail.message.$event is a common source of errors for newcomers.'app-notification')
Click the button to dispatch a custom event named 'app-notification' globally.
The payload will include a message and a timestamp.
window)
This panel is part of the main Comm_CustomEvents_Dispatch_example component.
It listens for 'app-notification' using @app-notification.window="handleAppNotification($event.detail)" in its root element.
Message:
Timestamp:
Source:
This is a completely separate Alpine component, also listening for 'app-notification' on the window object.
This demonstrates communication between non-ancestor components.