|
2 | 2 |
|
3 | 3 | The `cable` component is designed to asynchronously manipulate its DOM based on ActionCable events triggered on the serverside. Imagine a list of Todos and a `form` below that list. After creating a new Todo, the new list item can be rendered on the serverside and pushed to the `cable` component, which can be configured to pre or append the current list with the new item. Unlike the `async` component, the `cable` component does not request and rerender the whole list after receiving a specific event.
|
4 | 4 |
|
5 |
| -Furthermore you can update or remove items in that list using ActionCable events as well. The `cable` component again will only manipulate the specific DOM elements and not the whole list. This requires more implementation effort but gives you more flexibility and performance while creating reactive UIs compared to the `async` component. As usual, no JavaScript is required at your end in order to implement this sophisticated reactivity. |
| 5 | +Furthermore you can update or remove items in that list using ActionCable events as well. The `cable` component again will only manipulate the specific DOM elements and not the whole list. This requires more implementation effort but gives you more flexibility and performance while creating reactive UIs compared to the `async` component. As usual, no JavaScript is required at your end in order to implement this sophisticated reactivity. (Only one time setup as shown below) |
6 | 6 |
|
7 | 7 | Please read the [ActionCable Guide](/docs/guides/1000-action_cable/README.md) if you need help setting up ActionCable for your project.
|
8 | 8 |
|
| 9 | +Please make sure to setup ActionCable correctly. Esspecially following implementation is important in order to use the `cable` component correctly: |
| 10 | + |
| 11 | +`app/javascript/channels/matestack_ui_core_channel.js` |
| 12 | + |
| 13 | +```javascript |
| 14 | +consumer.subscriptions.create("MatestackUiCoreChannel", { |
| 15 | + //... |
| 16 | + received(data) { |
| 17 | + MatestackUiCore.matestackEventHub.$emit(data.event, data) |
| 18 | + } |
| 19 | +}); |
| 20 | +``` |
| 21 | + |
| 22 | +ActionCable pushes data as JSON to the client. You need to make sure to pass this data correctly into the `matestackEventHub` after receiving ActionCable event. |
| 23 | + |
9 | 24 |
|
10 | 25 | ## `cable` vs `async` component
|
11 | 26 |
|
@@ -256,6 +271,20 @@ cable id: "shopping-cart", replace_on: "shopping_cart_updated" do
|
256 | 271 | end
|
257 | 272 | ```
|
258 | 273 |
|
| 274 | +### Event data as Array |
| 275 | + |
| 276 | +All above shown examples demonstrated how to push a single component or ID to the `cable` component. In all usecases it's also possble to provide an Array of components/ID-strings, e.g.: |
| 277 | + |
| 278 | +```ruby |
| 279 | +ActionCable.server.broadcast("matestack_ui_core", { |
| 280 | + event: "todo_updated", |
| 281 | + data: [ |
| 282 | + matestack_component(:todo_component, todo: @todo1), |
| 283 | + matestack_component(:todo_component, todo: @todo2), |
| 284 | + ] |
| 285 | +}) |
| 286 | +``` |
| 287 | + |
259 | 288 |
|
260 | 289 | ## Complete documentation
|
261 | 290 |
|
|
0 commit comments