|
| 1 | +Params pass data downwards from owner to owned-by component. Data comes back upwards asynchronously |
| 2 | +via *callbacks*, which are simply *Procs* passed as params into the owned-by component. |
| 3 | + |
| 4 | +> **[More on Ruby Procs here ...](notes.md#ruby-procs)** |
| 5 | +
|
| 6 | +The upwards flow of data via callbacks is triggered by some event such as a mouse click, or input change: |
| 7 | + |
| 8 | +```ruby |
| 9 | +class ClickDemo2 < HyperComponent |
| 10 | + render do |
| 11 | + BUTTON { "click" }.on(:click) { |evt| puts "I was clicked"} |
| 12 | + end |
| 13 | +end |
| 14 | +``` |
| 15 | + |
| 16 | +When the `BUTTON` is clicked, the event (evt) is passed to the attached click handler. |
| 17 | + |
| 18 | +The details of the event object will be discussed below. |
| 19 | + |
| 20 | +### Firing Events from Components |
| 21 | + |
| 22 | +You can also define events in your components to communicate back to the owner: |
| 23 | + |
| 24 | +```ruby |
| 25 | +class Clicker < HyperComponent |
| 26 | + param title: "click" |
| 27 | + fires :clicked |
| 28 | + before_mount { @clicks = 0 } |
| 29 | + render do |
| 30 | + BUTTON { title }.on(:click) { clicked!(@clicks += 1) } |
| 31 | + end |
| 32 | +end |
| 33 | + |
| 34 | +class ClickDemo3 < HyperComponent |
| 35 | + render(DIV) do |
| 36 | + DIV { "I have been clicked #{pluralize(@clicks, 'times')}" } if @clicks |
| 37 | + Clicker().on(:clicked) { |clicks| mutate @clicks = clicks } |
| 38 | + end |
| 39 | +end |
| 40 | +``` |
| 41 | + |
| 42 | +Each time the `Clicker's` button is clicked it *fires* the clicked event, indicated |
| 43 | +by the event name followed by a bang (!). |
| 44 | + |
| 45 | +The `clicked` event is received by `ClickDemo3`, and it updates its state. As you |
| 46 | +can see events can send arbitrary data back out. |
| 47 | + |
| 48 | +> Notice also that Clicker does not call mutate. It could, but since the change in |
| 49 | +`@clicks` is not used anywhere to control its display there is no need for Clicker |
| 50 | +to mutate. |
| 51 | + |
| 52 | +### Relationship between Events and Params |
| 53 | + |
| 54 | +Notice how events (and callbacks in general as we will see) move data upwards, while |
| 55 | +params move data downwards. We can emphasize this by updating our example: |
| 56 | + |
| 57 | +```ruby |
| 58 | +class ClickDemo4 < HyperComponent |
| 59 | + def title |
| 60 | + @clicks ? "Click me again!" : "Let's start clicking!" |
| 61 | + end |
| 62 | + |
| 63 | + render(DIV) do |
| 64 | + DIV { "I have been clicked #{pluralize(@clicks, 'times')}" } if @clicks |
| 65 | + Clicker(title: title).on(:clicked) { |clicks| mutate @clicks = clicks } |
| 66 | + end |
| 67 | +end |
| 68 | +``` |
| 69 | + |
| 70 | +When `ClickDemo4` is first rendered, the `title` method will return "Let's start clicking!", and |
| 71 | +will be passed to `Clicker`. |
| 72 | + |
| 73 | +The user will (hopefully so we can get on with this chapter) click the button, which will |
| 74 | +fire the event. The handler in `ClickDemo4` will mutate its state, causing title to change |
| 75 | +to "Click me again!". The new value of the title param will be passed to `Clicker`, and `Clicker` |
| 76 | +will re-render with the new title. |
| 77 | + |
| 78 | +**Events (and callbacks) push data up, params move data down.** |
| 79 | + |
| 80 | +### Callbacks and Proc Params |
| 81 | + |
| 82 | +Under the hood Events are simply params of type `Proc`, with the `on` and `fires` method |
| 83 | +using some naming conventions to clean things up: |
| 84 | + |
| 85 | +```ruby |
| 86 | +class IntemittentButton < HyperComponent |
| 87 | + param :frequency |
| 88 | + param :pulse, type: Proc |
| 89 | + before_mount { @clicks = 0 } |
| 90 | + render do |
| 91 | + BUTTON( |
| 92 | + on_click: lambda {} do |
| 93 | + @clicks += 1 |
| 94 | + pulse(@clicks) if (@clicks % frequency).zero? |
| 95 | + end |
| 96 | + ) { 'click me' } |
| 97 | + end |
| 98 | +end |
| 99 | + |
| 100 | +class ClickDemo5 < HyperComponent |
| 101 | + render do |
| 102 | + IntermittentButton( |
| 103 | + frequency: 5, |
| 104 | + pulse: -> (total_clicks) { alert "you are clicking a lot" } |
| 105 | + ) |
| 106 | + end |
| 107 | +end |
| 108 | +``` |
| 109 | + |
| 110 | +There is really no reason not to use the `fires` method to declare Proc params, and |
| 111 | +no reason not use the `on` method to attach handlers. Both will keep your code clean and tidy. |
| 112 | + |
| 113 | +### Naming Conventions |
| 114 | + |
| 115 | +The notation `on(:click)` is short for passing a proc to a param named `on_click`. In general `on(:xxx)` will pass the |
| 116 | +given block as the `on_xxx` parameter in a Hyperstack component and `onXxx` in a JS component. |
| 117 | + |
| 118 | +All the built-in events and many React libraries follow the `on_xxx` (or `onXxx` in JS.) However even if a library does not use |
| 119 | +this convention you can still attach the handler using `on('<name-of-param>')`. Whatever string is inside the `<..>` brackets will |
| 120 | +be used as the param name. |
| 121 | + |
| 122 | +Likewise the `fires` method is shorthand for creating a `Proc` param following the `on_xxx` naming convention: |
| 123 | + |
| 124 | +`fires :foo` is short for |
| 125 | +`param :foo, type: Proc, alias: :foo!` |
| 126 | + |
| 127 | +### The `Event` Object |
| 128 | + |
| 129 | +UI events like `click` send an object of class `Event` to the handler. Some of the data you can get from `Event` objects are: |
| 130 | + |
| 131 | ++ `target` : the DOM object that was the target of the UI interaction |
| 132 | ++ `target.value` : the value of the DOM object |
| 133 | ++ `key_code` : the key pressed (for key_down and key_up events) |
| 134 | + |
| 135 | +> **[Refer to the Predefined Events section for complete details...](predefined-events.md)** |
| 136 | +
|
| 137 | +### Other Sources of Events |
| 138 | + |
| 139 | +Besides the UI there are several other sources of events: |
| 140 | + |
| 141 | ++ Timers |
| 142 | ++ HTTP Requests |
| 143 | ++ Hyperstack Operations |
| 144 | ++ Websockets |
| 145 | ++ Web Workers |
| 146 | + |
| 147 | +The way you receive events from these sources depends on the event. Typically though the method will either take a block, or callback proc, or in many cases will return a Promise. |
| 148 | +Regardless the event handler will do one of three things: mutate some state within the component, fire an event to a higher level component, or update some shared store. |
| 149 | + |
| 150 | +> For details on updating shared stores, which is often the best answer **[see the chapter on HyperState...](/hyper-state.md)** |
| 151 | +
|
| 152 | +You have seen the `every` method used to create events throughout this chapter, here is an example with an HTTP post (which returns a promise.) |
| 153 | + |
| 154 | +```ruby |
| 155 | +class SaveButton < HyperComponent |
| 156 | + fires :saved |
| 157 | + fires :failed |
| 158 | + render do |
| 159 | + BUTTON { "Save" } |
| 160 | + .on(:click) do |
| 161 | + # Posting to some non-hyperstack endpoint for example |
| 162 | + # Data is our class holding some data |
| 163 | + Hyperstack::HTTP.post( |
| 164 | + END_POINT, payload: Data.to_payload |
| 165 | + ).then do |response| |
| 166 | + saved!(response.json) |
| 167 | + end.fail do |response| |
| 168 | + failed!(response.json) |
| 169 | + end |
| 170 | + end |
| 171 | + end |
| 172 | +end |
| 173 | +``` |
0 commit comments