Class: Live::Element

Inherits:
Object
  • Object
show all
Defined in:
lib/live/element.rb

Overview

Represents a single dynamic content area on the page.

Direct Known Subclasses

View

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#idObject (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

#closeObject



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