Sha256: 1c9ad200d1129122131d442a9a97ab2b129e7d716b4de8fc288b714088f88e5c
Contents?: true
Size: 1.94 KB
Versions: 3
Compression:
Stored size: 1.94 KB
Contents
# frozen_string_literal: true # Released under the MIT License. # Copyright, 2021-2024, by Samuel Williams. require 'json' require 'securerandom' module Live # Represents a single dynamic content area on the page. class Element def self.unique_id SecureRandom.uuid end # @parameter id [String] The unique identifier within the page. # @parameter data [Hash] The data associated with the element, typically stored as `data-` attributes. def initialize(id = Element.unique_id, **data) @id = id @data = data @data[:class] ||= self.class.name @page = nil end # The unique id within the bound page. attr :id # The data associated with the element. attr :data # Generate a JavaScript string which forwards the specified event to the server. # @parameter detail [Hash] The detail associated with the forwarded event. def forward_event(detail = nil) if detail "live.forwardEvent(#{JSON.dump(@id)}, event, #{JSON.dump(detail)})" else "live.forwardEvent(#{JSON.dump(@id)}, event)" end end def forward_form_event(detail = nil) if detail "live.forwardFormEvent(#{JSON.dump(@id)}, event, #{JSON.dump(detail)})" else "live.forwardFormEvent(#{JSON.dump(@id)}, event)" end end # Bind this tag to a dynamically updating page. # @parameter page [Live::Page] def bind(page) @page = page end def close @page = nil end # Handle a client event, typically as triggered by {#forward}. # @parameter event [String] The type of the event. def handle(event) end # Enqueue a remote procedure call to the currently bound page. # @parameter method [Symbol] The name of the remote functio to invoke. # @parameter arguments [Array] def rpc(*arguments) # This update might not be sent right away. Therefore, mutable arguments may be serialized to JSON at a later time (or never). This could be a race condition: @page.updates.enqueue(arguments) end end end
Version data entries
3 entries across 3 versions & 1 rubygems
Version | Path |
---|---|
live-0.9.0 | lib/live/element.rb |
live-0.8.1 | lib/live/element.rb |
live-0.8.0 | lib/live/element.rb |