CableReady
Search…
DOM Events

dispatch_event

Create a custom DOM event in the browser for the purpose of implementing custom functionality.
dispatch_event immediately dispatches a DOM CustomEvent in the browser. These events bubble up through the DOM and are cancelable.
If jQuery is in use (and available in the global window scope) dispatch_event will also trigger a jQuery event with the same name.
1
cable_ready["MyChannel"].dispatch_event(
2
name: "string", # required - the name of the DOM event to dispatch (can be custom)
3
detail: {}, # [null] - assigned to event.detail
4
select_all: true|false, # [false] - operate on list of elements returned from selector
5
selector: "string", # [document] - string containing a CSS selector or XPath expression
6
xpath: true|false # [false] - process the selector as an XPath expression
7
)
Copied!
While the event itself is frequently the payload, you can build very powerful functionality by passing a Hash of metadata to the detail parameter.
The detail parameter will convert user_id to userId - snake_case to camelCase - for JS consumption on the client.
Developers frequently use dispatch_event to notify the client when long-running server processes are completed. You can see an example in Leveraging Stimulus.
There are no life-cycle events emitted for dispatch_event.

Reference

Last modified 9mo ago
Export as PDF
Copy link