Skip to content

Commit 69285aa

Browse files
committed
[TASK] adjusted docs for cable component due to review feedback
1 parent 190c712 commit 69285aa

File tree

2 files changed

+47
-5
lines changed

2 files changed

+47
-5
lines changed

docs/api/100-components/cable.md

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,26 @@
22

33
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.
44

5+
Please read the [ActionCable Guide](/docs/guides/1000-action_cable/README.md) if you need help setting up ActionCable for your project.
6+
7+
Please make sure to setup ActionCable correctly. Esspecially following implementation is important in order to use the `cable` component correctly:
8+
9+
`app/javascript/channels/matestack_ui_core_channel.js`
10+
11+
```javascript
12+
consumer.subscriptions.create("MatestackUiCoreChannel", {
13+
//...
14+
received(data) {
15+
MatestackUiCore.matestackEventHub.$emit(data.event, data)
16+
}
17+
});
18+
```
19+
520
## `cable(*args, &block)`
621
----
722

823
Returns a vue.js driven cable component initially containing content specified by a block.
924

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-
1225
Imagine something like this:
1326

1427
```ruby
@@ -44,7 +57,7 @@ class ListComponent < Matestack::Ui::Component
4457
end
4558
```
4659

47-
60+
Please notice: when rendering a list of items, we recommend to use a custom component for each item. This makes it easy to render unique items on the serverside and push them via ActionCable to the client. But it is possible to also use another component or a html string. Any given html will be used to update the list.
4861

4962

5063
**Required options**
@@ -172,4 +185,4 @@ end
172185
`data` can also be an Array of ID-strings
173186

174187

175-
* Html attributes - all w3c confirm html attributes for div's can be set via options and will be added to the surrounding card div.
188+
* Html attributes - all w3c confirm html attributes for div's can be set via options and will be added to the surrounding card div.

docs/guides/750-cable/README.md

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,25 @@
22

33
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.
44

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)
66

77
Please read the [ActionCable Guide](/docs/guides/1000-action_cable/README.md) if you need help setting up ActionCable for your project.
88

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+
924

1025
## `cable` vs `async` component
1126

@@ -256,6 +271,20 @@ cable id: "shopping-cart", replace_on: "shopping_cart_updated" do
256271
end
257272
```
258273

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+
259288

260289
## Complete documentation
261290

0 commit comments

Comments
 (0)