|
16 | 16 | <script src="https://rawgit.com/ruby-hyperloop/hyperloop-js/master/hyperloop.min.js"></script>
|
17 | 17 |
|
18 | 18 | <script type="text/ruby">
|
19 |
| - class SimpleComponent < Hyperloop::Component |
| 19 | + class TopLevelComponent < Hyperloop::Component |
20 | 20 | render(DIV) do
|
21 |
| - BUTTON { 'Push the button' }.on(:click) do |
22 |
| - alert 'You did it!' |
| 21 | + H1 { "Components, Stores and Operations" } |
| 22 | + TypeAlong() |
| 23 | + ButtonComponent() |
| 24 | + Clock() |
| 25 | + end |
| 26 | + end |
| 27 | + |
| 28 | + class MyStore < Hyperloop::Store |
| 29 | + state :value, reader: true, scope: :class |
| 30 | + |
| 31 | + def self.set_value value |
| 32 | + mutate.value value |
| 33 | + end |
| 34 | + |
| 35 | + def self.clear |
| 36 | + mutate.value "" |
| 37 | + end |
| 38 | + end |
| 39 | + |
| 40 | + class ClearOp < Hyperloop::Operation |
| 41 | + step { puts "about to clear everything" } |
| 42 | + step { MyStore.clear } |
| 43 | + end |
| 44 | + |
| 45 | + class ButtonComponent < Hyperloop::Component |
| 46 | + render(DIV) do |
| 47 | + H2 { "Here is a button" } |
| 48 | + BUTTON { 'See the value' }.on(:click) do |
| 49 | + alert "MyStore value is '#{ MyStore.value }' " |
23 | 50 | end
|
| 51 | + BUTTON { 'Clear' }.on(:click) { ClearOp.run } |
| 52 | + end |
| 53 | + end |
| 54 | + |
| 55 | + class TypeAlong < Hyperloop::Component |
| 56 | + render(DIV) do |
| 57 | + H2 { "MyStore value is '#{ MyStore.value }'" } |
| 58 | + INPUT(type: :text, value: MyStore.value ).on(:change) do |e| |
| 59 | + MyStore.set_value e.target.value |
| 60 | + end |
| 61 | + end |
| 62 | + end |
| 63 | + |
| 64 | + class Clock < Hyperloop::Component |
| 65 | + param format: '%a, %e %b %Y %H:%M:%S' |
| 66 | + before_mount do |
| 67 | + mutate.time Time.now.strftime(params.format) |
| 68 | + every(1) { mutate.time Time.now.strftime(params.format) } |
| 69 | + end |
| 70 | + |
| 71 | + render(DIV) do |
| 72 | + H2 { "And a clock" } |
| 73 | + "The time is #{state.time}" |
24 | 74 | end
|
25 | 75 | end
|
26 | 76 | </script>
|
| 77 | + |
27 | 78 | </head>
|
28 | 79 |
|
29 | 80 | <body>
|
30 |
| - <div data-hyperloop-mount="SimpleComponent"> |
31 |
| - </div> |
| 81 | + <div data-hyperloop-mount="TopLevelComponent"></div> |
32 | 82 | </body>
|
33 | 83 | </html>
|
0 commit comments