Module: LiveCable::Component::Events
- Extended by:
- ActiveSupport::Concern
- Included in:
- LiveCable::Component
- Defined in:
- lib/live_cable/component/events.rb
Instance Method Summary collapse
-
#dispatch_event(name, positional_detail = nil, window: false, **detail) ⇒ Object
Queue a DOM event to be dispatched on the client.
-
#flush_events ⇒ Array<Hash>
Drain the queued events.
Instance Method Details
#dispatch_event(name, positional_detail = nil, window: false, **detail) ⇒ Object
Queue a DOM event to be dispatched on the client. Events are delivered with the next broadcast for this component - attached to the render when state changed, or on their own when it didn't - and fire on the client after the DOM has been morphed, so handlers see the updated markup.
On the client the event is a bubbling CustomEvent dispatched from the component's root element (or from window with window: true), so it can be handled with plain Stimulus data-action syntax:
25 26 27 28 29 |
# File 'lib/live_cable/component/events.rb', line 25 def dispatch_event(name, positional_detail = nil, window: false, **detail) detail = positional_detail if positional_detail pending_events << { name: name.to_s, detail: detail.as_json, window: } end |
#flush_events ⇒ Array<Hash>
Drain the queued events. Called when a broadcast is sent so each event is delivered exactly once.
35 36 37 38 39 |
# File 'lib/live_cable/component/events.rb', line 35 def flush_events events = pending_events.dup pending_events.clear events end |