|
| 1 | +# Matestack Core Component: Cable |
| 2 | + |
| 3 | +The `cable` component enables us to update the DOM based on events and data pushed via ActionCable without browser reload. Please read the [ActionCable Guide](/docs/guides/1000-action_cable/README.md) if you need help setting up ActionCable for your project. |
| 4 | + |
| 5 | +## `cable(*args, &block)` |
| 6 | +---- |
| 7 | + |
| 8 | +Returns a vue.js driven cable component initially containing content specified by a block. |
| 9 | + |
| 10 | +When rendering a list of items, make sure to use a custom component for each item. This is mandatory in order to render unique items on the serverside and push them via ActionCable to the client. |
| 11 | + |
| 12 | +Imagine something like this: |
| 13 | + |
| 14 | +```ruby |
| 15 | +class MyPage < Matestack::Ui::Page |
| 16 | + |
| 17 | + def response |
| 18 | + cable id: 'foo' [...] do |
| 19 | + #this block will be rendered as initial content |
| 20 | + #and may be modified on the client later on when receiving defined events |
| 21 | + DummyModel.all.each do |instance| |
| 22 | + my_list_component item: instance |
| 23 | + end |
| 24 | + end |
| 25 | + end |
| 26 | +end |
| 27 | +``` |
| 28 | + |
| 29 | +with a (registered!) custom component: |
| 30 | + |
| 31 | +```ruby |
| 32 | +class MyListComponent < Matestack::Ui::Component |
| 33 | + |
| 34 | + requires :item |
| 35 | + |
| 36 | + def response |
| 37 | + #make sure to define a unique ID on the root element of your component |
| 38 | + #this ID may be used to update or delete this component on the client later on |
| 39 | + div id: "item-#{item.id}", class: "row" do |
| 40 | + #... |
| 41 | + end |
| 42 | + end |
| 43 | + |
| 44 | +end |
| 45 | +``` |
| 46 | + |
| 47 | +**Required options** |
| 48 | + |
| 49 | +* `id` - Expects a unique string identifying the component |
| 50 | + |
| 51 | +**Optional options** |
| 52 | + |
| 53 | +* `append_on` - Expects a string which matches the event which will be emitted via ActionCable on the serverside. Event payload data in form of HTML will be **appended** to the current cable component DOM |
| 54 | + |
| 55 | + On your UI class: |
| 56 | + |
| 57 | + ```ruby |
| 58 | + cable id: 'foo', append_on: "model_created" do |
| 59 | + DummyModel.all.each do |instance| |
| 60 | + my_list_component item: instance |
| 61 | + end |
| 62 | + end |
| 63 | + ``` |
| 64 | + |
| 65 | + On your controller: |
| 66 | + |
| 67 | + ```ruby |
| 68 | + ActionCable.server.broadcast("matestack_ui_core", { |
| 69 | + event: "model_created", |
| 70 | + data: matestack_component(:my_list_component, item: @new_model_instance) |
| 71 | + }) |
| 72 | + ``` |
| 73 | + |
| 74 | + `data` can also be an Array of components |
| 75 | + |
| 76 | + |
| 77 | +* `prepend_on` - Expects a string which matches the event which will be emitted via ActionCable on the serverside. Event payload data in form of HTML will be **prepended** to the current cable component DOM |
| 78 | + |
| 79 | + On your UI class: |
| 80 | + |
| 81 | + ```ruby |
| 82 | + cable id: 'foo', prepend_on: "model_created" do |
| 83 | + DummyModel.all.each do |instance| |
| 84 | + my_list_component item: instance |
| 85 | + end |
| 86 | + end |
| 87 | + ``` |
| 88 | + |
| 89 | + On your controller: |
| 90 | + |
| 91 | + ```ruby |
| 92 | + ActionCable.server.broadcast("matestack_ui_core", { |
| 93 | + event: "model_created", |
| 94 | + data: matestack_component(:my_list_component, item: @new_model_instance) |
| 95 | + }) |
| 96 | + ``` |
| 97 | + |
| 98 | + `data` can also be an Array of components |
| 99 | + |
| 100 | + |
| 101 | +* `replace_on` - Expects a string which matches the event which will be emitted via ActionCable on the serverside. Event payload data in form of HTML will **replace** the whole current cable component DOM |
| 102 | + |
| 103 | + On your UI class: |
| 104 | + |
| 105 | + ```ruby |
| 106 | + cable id: 'foo', replace_on: "model_created" do |
| 107 | + my_list_component item: instance |
| 108 | + end |
| 109 | + ``` |
| 110 | + |
| 111 | + On your controller: |
| 112 | + |
| 113 | + ```ruby |
| 114 | + ActionCable.server.broadcast("matestack_ui_core", { |
| 115 | + event: "model_created", |
| 116 | + data: matestack_component(:my_list_component, item: @new_model_instance) |
| 117 | + }) |
| 118 | + ``` |
| 119 | + |
| 120 | + `data` can also be an Array of components |
| 121 | + |
| 122 | + |
| 123 | + |
| 124 | +* `update_on` - Expects a string which matches the event which will be emitted via ActionCable on the serverside. Event payload data in form of HTML will **update** a specific element iditified by its root ID within the current cable component DOM |
| 125 | + |
| 126 | + On your UI class: |
| 127 | + |
| 128 | + ```ruby |
| 129 | + cable id: 'foo', append_on: "model_created" do |
| 130 | + DummyModel.all.each do |instance| |
| 131 | + my_list_component item: instance |
| 132 | + end |
| 133 | + end |
| 134 | + ``` |
| 135 | + |
| 136 | + On your controller: |
| 137 | + |
| 138 | + ```ruby |
| 139 | + ActionCable.server.broadcast("matestack_ui_core", { |
| 140 | + event: "model_created", |
| 141 | + data: matestack_component(:my_list_component, item: @new_model_instance) |
| 142 | + }) |
| 143 | + ``` |
| 144 | + |
| 145 | + `data` can also be an Array of components |
| 146 | + |
| 147 | + |
| 148 | +* `delete_on` - Expects a string which matches the event which will be emitted via ActionCable on the serverside. Event payload data in form of a string containing the ID will **remove** a specific element identified by its root ID within the current cable component DOM |
| 149 | + |
| 150 | + On your UI class: |
| 151 | + |
| 152 | + ```ruby |
| 153 | + cable id: 'foo', delete_on: "model_deleted" do |
| 154 | + DummyModel.all.each do |instance| |
| 155 | + my_list_component item: instance |
| 156 | + end |
| 157 | + end |
| 158 | + ``` |
| 159 | + |
| 160 | + On your controller: |
| 161 | + |
| 162 | + ```ruby |
| 163 | + ActionCable.server.broadcast("matestack_ui_core", { |
| 164 | + event: "model_deleted", |
| 165 | + data: "item-#{params[:id]}" |
| 166 | + }) |
| 167 | + ``` |
| 168 | + |
| 169 | + `data` can also be an Array of ID-strings |
| 170 | + |
| 171 | + |
| 172 | +* Html attributes - all w3c confirm html attributes for div's can be set via options and will be added to the surrounding card div. |
0 commit comments