Class: Live::Element
- Inherits:
-
Object
- Object
- Live::Element
- Defined in:
- lib/live/element.rb
Overview
Represents a single dynamic content area on the page.
Direct Known Subclasses
Instance Attribute Summary collapse
-
#id ⇒ Object
readonly
The unique id within the bound page.
Instance Method Summary collapse
-
#bind(page) ⇒ Object
Bind this tag to a dynamically updating page.
- #close ⇒ Object
-
#forward(details = nil) ⇒ Object
Generate a JavaScript string which forwards the specified event to the server.
-
#handle(event) ⇒ Object
Handle a client event, typically as triggered by #forward.
-
#initialize(id, **data) ⇒ Element
constructor
A new instance of Element.
-
#rpc(method, arguments) ⇒ Object
Enqueue a remote procedure call to the currently bound page.
Constructor Details
#initialize(id, **data) ⇒ Element
Returns a new instance of Element.
30 31 32 33 34 35 36 |
# File 'lib/live/element.rb', line 30 def initialize(id, **data) @id = id @data = data @data[:class] ||= self.class.name @page = nil end |
Instance Attribute Details
#id ⇒ Object (readonly)
The unique id within the bound page.
39 40 41 |
# File 'lib/live/element.rb', line 39 def id @id end |
Instance Method Details
#bind(page) ⇒ Object
Bind this tag to a dynamically updating page.
53 54 55 |
# File 'lib/live/element.rb', line 53 def bind(page) @page = page end |
#close ⇒ Object
57 58 59 |
# File 'lib/live/element.rb', line 57 def close @page = nil end |
#forward(details = nil) ⇒ Object
Generate a JavaScript string which forwards the specified event to the server.
43 44 45 46 47 48 49 |
# File 'lib/live/element.rb', line 43 def forward(details = nil) if details "live.forward(#{JSON.dump(@id)}, event, #{JSON.dump(details)})" else "live.forward(#{JSON.dump(@id)}, event)" end end |
#handle(event) ⇒ Object
Handle a client event, typically as triggered by #forward.
63 64 |
# File 'lib/live/element.rb', line 63 def handle(event) end |
#rpc(method, arguments) ⇒ Object
Enqueue a remote procedure call to the currently bound page.
69 70 71 72 |
# File 'lib/live/element.rb', line 69 def rpc(method, 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([method, arguments]) end |