Skip to content

Commit c58b4f0

Browse files
committed
Adjust the Component for Post Rerendering
1 parent 051c945 commit c58b4f0

File tree

1 file changed

+16
-10
lines changed

1 file changed

+16
-10
lines changed

docs/getting-started/quick-start.md

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1064,16 +1064,16 @@ class Components::Post < Matestack::Ui::Component
10641064
requires :post
10651065

10661066
def response
1067-
# async rerender_on: "cable__liked_post_#{post.id}", id: "post-#{post.id}" do
1068-
div class: "mb-3 p-3 rounded shadow-sm", id: "post-#{post.id}" do
1067+
# async rerender_on: "cable__liked_post_#{post.id}", id: "post-#{context.post.id}" do
1068+
div class: "mb-3 p-3 rounded shadow-sm", id: "post-#{context.post.id}" do
10691069
heading size: 5 do
1070-
plain post.username
1071-
small text: post.created_at.strftime("%d.%m.%Y %H:%M")
1070+
plain context.post.username
1071+
small text: context.post.created_at.strftime("%d.%m.%Y %H:%M")
10721072
end
1073-
paragraph text: post.body, class: "mb-5"
1074-
action path: like_post_path(post), method: :put do
1073+
paragraph text: context.post.body, class: "mb-5"
1074+
action path: like_post_path(context.post), method: :put do
10751075
button class: "btn btn-light" do
1076-
plain "Like (#{post.likes_count})"
1076+
plain "Like (#{context.post.likes_count})"
10771077
end
10781078
end
10791079
end
@@ -1085,6 +1085,8 @@ end
10851085

10861086
* [x] Adjust the ActionCable broadcast on the `like` action on the post controller
10871087

1088+
# todo: cable__liked_post not working here
1089+
10881090
`app/controllers/posts_controller.rb`
10891091

10901092
```ruby
@@ -1101,7 +1103,7 @@ def like
11011103
# no id required in the event name, the cable component will figure out which post
11021104
# should be updated using the root element ID of the pushed component
11031105
event: "cable__liked_post", # change the event name
1104-
data: matestack_component(:post_component, post: @post) # add this line
1106+
data: post_component(post: @post) # add this line
11051107
})
11061108
render json: {
11071109
message: 'Post was successfully liked.'
@@ -1121,7 +1123,7 @@ def create
11211123
if @post.save
11221124
ActionCable.server.broadcast('matestack_ui_core', {
11231125
event: 'cable__created_post',
1124-
data: matestack_component(:post_component, post: @post) # add this line
1126+
data: post_component(post: @post)
11251127
})
11261128
render json: {
11271129
message: 'Post was successfully created.'
@@ -1144,7 +1146,7 @@ end
11441146

11451147
Again: you probably don't realize any difference on the UI, but now ONLY the updated post will be rendered on the server and pushed to the `cable` component mounted in the browser.
11461148

1147-
The `cable` component is configured to `updated` the component pushed from the server on the `cable__liked_post` event. The `cable` component then reads the ID of the root element of the pushed component, looks for that ID within it's body and updates this element with the pushed component.
1149+
The `cable` component is configured to `update` the component pushed from the server on the `cable__liked_post` event. The `cable` component then reads the ID of the root element of the pushed component, looks for that ID within it's body and updates this element with the pushed component.
11481150

11491151
Now, we're rerendering the list and its elements completely with the `cable` component. As described, this is an ALTERNATIVE approach to the introduced `async` component approach. The `cable` component requires a bit more implementation and brain power but makes our reactivity more scalable. Use the `cable` component wherever you think `async` would be too slow at some point!
11501152

@@ -1576,6 +1578,8 @@ And now we do something, what's not possible in Twitter: Editing. Tweets. Inline
15761578

15771579
`app/matestack/components/post.rb`
15781580

1581+
# todo: context.post
1582+
15791583
```ruby
15801584
class Components::Post < Matestack::Ui::Component
15811585

@@ -1650,6 +1654,8 @@ end
16501654

16511655
`app/controllers/posts_controller.rb`
16521656

1657+
# todo: matestack_component
1658+
16531659
```ruby
16541660
# ...
16551661

0 commit comments

Comments
 (0)