Class: Lively::HelloWorld

Inherits:
Live::View
  • Object
show all
Defined in:
lib/lively/hello_world.rb

Overview

Represents a “Hello World” Live view component.

This component displays a simple greeting message with a real-time clock that updates every second. It serves as a demonstration of Lively’s live update capabilities and provides basic usage instructions.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeHelloWorld

Initialize a new HelloWorld view.



16
17
18
19
20
# File 'lib/lively/hello_world.rb', line 16

def initialize(...)
	super
	
	@clock = nil
end

Instance Attribute Details

#clockObject (readonly)

Returns the value of attribute clock.



23
24
25
# File 'lib/lively/hello_world.rb', line 23

def clock
  @clock
end

Instance Method Details

#bind(page) ⇒ Object

Bind this view to a page and start the update clock.



27
28
29
30
31
32
33
34
35
36
37
# File 'lib/lively/hello_world.rb', line 27

def bind(page)
	super
	
	@clock ||= Async do
		while true
			self.update!
			
			sleep 1
		end
	end
end

#closeObject

Close this view and stop the update clock.



40
41
42
43
44
# File 'lib/lively/hello_world.rb', line 40

def close
	@clock&.stop
	
	super
end

#render(builder) ⇒ Object

Render this view as HTML.



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/lively/hello_world.rb', line 48

def render(builder)
	builder.tag(:h1) do
		builder.text("Hello, I'm Lively!")
	end
	
	builder.tag(:p) do
		builder.text("The time is #{Time.now}.")
	end
	
	builder.tag(:p) do
		builder.text(<<~TEXT)
			Lively is a simple client-server SPA framework. It is designed to be easy to use and understand, while providing a solid foundation for building interactive web applications. Create an `application.rb` file and define your own `Application` class to get started.
		TEXT
	end
	
	builder.inline_tag(:pre) do
		builder.text(<<~TEXT)
			#!/usr/bin/env lively
			
			class Application < Lively::Application
				def body
					Lively::HelloWorld.new
				end
			end
		TEXT
	end
	
	builder.tag(:p) do
		builder.text("Check the `examples/` directory for... you guessed it... more examples.")
	end
end

#The background task that updates the view periodically.=(backgroundtaskthatupdatestheviewperiodically. = (value)) ⇒ Object



23
# File 'lib/lively/hello_world.rb', line 23

attr :clock