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("      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.\n    TEXT\n  end\n  \n  builder.inline_tag(:pre) do\n    builder.text(<<~TEXT)\n      #!/usr/bin/env lively\n      \n      class Application < Lively::Application\n        def body\n          Lively::HelloWorld.new\n        end\n      end\n    TEXT\n  end\n  \n  builder.tag(:p) do\n    builder.text(\"Check the `examples/` directory for... you guessed it... more examples.\")\n  end\nend\n")

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



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

attr :clock