Skip to content

Commit 190c712

Browse files
author
Nils Henning
committed
[TASk] update cable guide/doc
1 parent 7ec5a62 commit 190c712

File tree

2 files changed

+34
-31
lines changed

2 files changed

+34
-31
lines changed

docs/api/100-components/cable.md

Lines changed: 28 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -12,24 +12,24 @@ When rendering a list of items, make sure to use a custom component for each ite
1212
Imagine something like this:
1313

1414
```ruby
15-
class MyPage < Matestack::Ui::Page
15+
class Page < Matestack::Ui::Page
1616

1717
def response
1818
cable id: 'foo' [...] do
1919
#this block will be rendered as initial content
2020
#and may be modified on the client later on when receiving defined events
2121
DummyModel.all.each do |instance|
22-
my_list_component item: instance
22+
list_component item: instance
2323
end
2424
end
2525
end
2626
end
2727
```
2828

29-
with a (registered!) custom component:
29+
with an as `list_component` registered component:
3030

3131
```ruby
32-
class MyListComponent < Matestack::Ui::Component
32+
class ListComponent < Matestack::Ui::Component
3333

3434
requires :item
3535

@@ -44,30 +44,33 @@ class MyListComponent < Matestack::Ui::Component
4444
end
4545
```
4646

47+
48+
49+
4750
**Required options**
4851

4952
* `id` - Expects a unique string identifying the component
5053

5154
**Optional options**
5255

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
56+
* `append_on` - Expects a string matching 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
5457

55-
On your UI class:
58+
In your app, page or component:
5659

5760
```ruby
5861
cable id: 'foo', append_on: "model_created" do
5962
DummyModel.all.each do |instance|
60-
my_list_component item: instance
63+
list_component item: instance
6164
end
6265
end
6366
```
6467

65-
On your controller:
68+
In your controller:
6669

6770
```ruby
6871
ActionCable.server.broadcast("matestack_ui_core", {
6972
event: "model_created",
70-
data: matestack_component(:my_list_component, item: @new_model_instance)
73+
data: matestack_component(:list_component, item: @new_model_instance)
7174
})
7275
```
7376

@@ -76,22 +79,22 @@ end
7679

7780
* `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
7881

79-
On your UI class:
82+
In your app, page or component:
8083

8184
```ruby
8285
cable id: 'foo', prepend_on: "model_created" do
8386
DummyModel.all.each do |instance|
84-
my_list_component item: instance
87+
list_component item: instance
8588
end
8689
end
8790
```
8891

89-
On your controller:
92+
In your controller:
9093

9194
```ruby
9295
ActionCable.server.broadcast("matestack_ui_core", {
9396
event: "model_created",
94-
data: matestack_component(:my_list_component, item: @new_model_instance)
97+
data: matestack_component(:list_component, item: @new_model_instance)
9598
})
9699
```
97100

@@ -100,20 +103,20 @@ end
100103

101104
* `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
102105

103-
On your UI class:
106+
In your app, page or component:
104107

105108
```ruby
106109
cable id: 'foo', replace_on: "model_created" do
107-
my_list_component item: instance
110+
list_component item: instance
108111
end
109112
```
110113

111-
On your controller:
114+
In your controller:
112115

113116
```ruby
114117
ActionCable.server.broadcast("matestack_ui_core", {
115118
event: "model_created",
116-
data: matestack_component(:my_list_component, item: @new_model_instance)
119+
data: matestack_component(:list_component, item: @new_model_instance)
117120
})
118121
```
119122

@@ -123,22 +126,22 @@ end
123126

124127
* `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
125128

126-
On your UI class:
129+
In your app, page or component:
127130

128131
```ruby
129132
cable id: 'foo', append_on: "model_created" do
130133
DummyModel.all.each do |instance|
131-
my_list_component item: instance
134+
list_component item: instance
132135
end
133136
end
134137
```
135138

136-
On your controller:
139+
In your controller:
137140

138141
```ruby
139142
ActionCable.server.broadcast("matestack_ui_core", {
140143
event: "model_created",
141-
data: matestack_component(:my_list_component, item: @new_model_instance)
144+
data: matestack_component(:list_component, item: @new_model_instance)
142145
})
143146
```
144147

@@ -147,17 +150,17 @@ end
147150

148151
* `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
149152

150-
On your UI class:
153+
In your app, page or component:
151154

152155
```ruby
153156
cable id: 'foo', delete_on: "model_deleted" do
154157
DummyModel.all.each do |instance|
155-
my_list_component item: instance
158+
list_component item: instance
156159
end
157160
end
158161
```
159162

160-
On your controller:
163+
In your controller:
161164

162165
```ruby
163166
ActionCable.server.broadcast("matestack_ui_core", {
@@ -169,4 +172,4 @@ end
169172
`data` can also be an Array of ID-strings
170173

171174

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

docs/guides/750-cable/README.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@
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 your 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.
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

99

1010
## `cable` vs `async` component
1111

12-
`cable` and `async` might seem similar. They indeed can be used for similar use cases - imagine implementing a reactive list of Todo items (ActiveRecord Instances) created via a `form` below the list. You can either use `async` or `cable` in order to create that reactive list!
12+
`cable` and `async` might seem similar. They indeed can be used for similar use cases - imagine implementing a reactive list of todo items (using ActiveRecord) created via a `form` below the list. You can either use `async` or `cable` in order to create that reactive list!
1313

1414
But they work completely differently:
1515

@@ -56,7 +56,7 @@ class MyPage < Matestack::Ui::Page
5656
end
5757
```
5858

59-
with a (registered!) custom component:
59+
with an as `todo_component` registered component:
6060

6161
```ruby
6262
class TodoComponent < Matestack::Ui::Component
@@ -74,9 +74,9 @@ class TodoComponent < Matestack::Ui::Component
7474
end
7575
```
7676

77-
After form submission, the form resets itself dynamically, but the list will not get updated with the new todo instance. You can now decide, if you want to use `async` or `cable` in order to implement that reactivity. `async` could react to an event emitted by the `form` and simply rerender the whole list without any further implementation. Wrapping the list in an correctly configured `async` component would be enough!
77+
After form submission, the form resets itself dynamically, but the list will not get updated with the new todo instance. You can now decide, if you want to use `async` or `cable` in order to implement that reactivity. `async` could react to an event emitted by the `form` and simply rerender the whole list without any further implementation. Wrapping the list in a correctly configured `async` component would be enough!
7878

79-
But in this case, we do not want to rerender the whole list every time we submitted the form, because - let's say - the list will be quite long and rerendering the whole list would be too slow. We only want to add new items to the current DOM without touching the rest of the list. The `cable` component enables you to do exactly this. The principle behind: After form submission the new component is rendered on the serverside and than pushed to the clientside via ActionCable. The `cable` component receives this event and will than - depending on your configuration - append or prepend the current list on the UI. Implementation would look like this:
79+
But in this case, we do not want to rerender the whole list every time we submitted the form, because - let's say - the list will be quite long and rerendering the whole list would be getting slow. We only want to add new items to the current DOM without touching the rest of the list. The `cable` component enables you to do exactly this. The principle behind it: After form submission the new component is rendered on the serverside and than pushed to the clientside via ActionCable. The `cable` component receives this event and will than - depending on your configuration - append or prepend the current list on the UI. Implementation would look like this:
8080

8181
### Appending elements
8282

@@ -121,7 +121,7 @@ end
121121

122122
```
123123

124-
Please notice that it is mandatory to use a component for each list item. Otherwise it wouldn't be possible to render only a new list item within the `create` action and push only that new component to the client.
124+
Please notice that we recommend to use a component for each list item. With a component for each item it is possible to easily render a new list item within the `create` action and push it to the client. But it is possible to also use another component or a html string. Any given html will be appended to the list.
125125

126126
### Prepending elements
127127

0 commit comments

Comments
 (0)