You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/built-in-reactivity/partial-ui-updates/async-component-api.md
+28-28Lines changed: 28 additions & 28 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -8,7 +8,7 @@ Please be aware that, if not configured otherwise, the `async` core component do
8
8
9
9
The `async` core component accepts the following parameters:
10
10
11
-
### ID - required
11
+
### ID - required
12
12
13
13
The `async` component needs an ID in order to resolve the correct content on an async HTTP request
14
14
@@ -32,13 +32,13 @@ end
32
32
33
33
**Note:** The `rerender_on` option lets you rerender parts of your UI asynchronously. But please consider that, if not configured differently, it
34
34
35
-
a\) is **not**_lazily loaded_ and
35
+
a) is **not**_lazily loaded_ and
36
36
37
-
b\) and does get displayed on initial pageload
37
+
b) and does get displayed on initial pageload
38
38
39
39
by default.
40
40
41
-
Lazy \(or defered\) loading can be configured like shown [here](async-component-api.md#defer).
41
+
Lazy (or defered) loading can be configured like shown [here](async-component-api.md#defer).
42
42
43
43
You can pass in multiple, comma-separated events on which the component should rerender.
44
44
@@ -127,43 +127,43 @@ During async rendering a `loading` class will automatically be applied, which ca
127
127
128
128
## Examples
129
129
130
-
See some common use cases below:
130
+
### Deferring content
131
131
132
-
### Rerender on event
133
-
134
-
On our example page, we wrap a simple timestamp in an async component and tell it to rerender when the event `my_event` gets triggered.
132
+
You can either configure an `async` component to request its content directly after the page load or to delay the request for a given amount of time after the page load. `:defer` expects either a boolean or a integer representing the delay time in milliseconds. If `:defer` is set to `false` the `async` component will be rendered on page load and not deferred. If set to `true` it will request its content directly after the page load.
plain 'Some content rendered after page is loaded.'
145
138
end
139
+
end
140
+
```
146
141
142
+
The above `async` component will be rendered asynchronously after page load.
143
+
144
+
```ruby
145
+
defresponse
146
+
async id:'delayed-deferred-async', defer:500do
147
+
plain 'Some delayed deferred content'
148
+
end
147
149
end
148
150
```
149
151
150
-
Not surprisingly, the timestamp gets updated after our event was fired!
152
+
Specifying `defer: 500` will delay the asynchronous request after page load of the `async` component for 300ms and render the content afterwards.
151
153
152
-
### Deferred loading
154
+
### Rerendering content
153
155
154
-
On our example page, we wrap our `async` event around a placeholder for the event message.
156
+
The `async` leverages the event hub and can react to emitted events. If it receives one or more of the with `:rerender_on` specified events it will asynchronously request a rerender of its content. The response will only include the rerendered html of the `async` component which then replaces the current content of the `async`. If you specify multiple events in `:rerender_on` they need to be seperated by a comma.
155
157
156
158
```ruby
157
-
classExamplePage < Matestack::Ui::Page
158
-
159
-
defresponse
160
-
async defer:true, id:"some-unique-id"do
161
-
div id:'my-div'do
162
-
plain 'I will be requested within a separate GET request right after initial page load is done'
The above snippet renders a paragraph with the current time and a button "Update time" on page load. If the button is clicked a _update-time_ event is emitted. The `async` component wrapping the paragraph receives the event and reacts to it by requesting its rerendered content from the server and replacing its content with the received html. In this case it will rerender after button click and show the updated time.
Imagine a list of Todo Items and a above that list a form to create new Todos, implemented like this:
195
+
196
+
```ruby
197
+
classMyPage < Matestack::Ui::Page
198
+
199
+
defresponse
200
+
matestack_form method::post, path: create_action_rails_route_path do
201
+
form_input key::title, type::text
202
+
button "submit"
203
+
end
204
+
205
+
Todo.all.each do |instance|
206
+
TodoComponent.call(todo: instance)
207
+
end
208
+
end
209
+
210
+
end
211
+
```
212
+
213
+
```ruby
214
+
classTodoComponent < Matestack::Ui::Component
215
+
216
+
required :todo
217
+
218
+
defresponse
219
+
div id:"todo-#{context.todo.id}", class: "row"do
220
+
div class: "col"do
221
+
plain context.todo.title
222
+
end
223
+
end
224
+
end
225
+
226
+
end
227
+
```
228
+
229
+
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!
230
+
231
+
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:
232
+
233
+
234
+
235
+
### Appending elements
236
+
237
+
_adding elements on the bottom of the list_
238
+
239
+
```ruby
240
+
classMyPage < Matestack::Ui::Page
241
+
242
+
defresponse
243
+
matestack_form method::post, path: create_action_rails_route_path do
# respond to the form POST request (needs to be last)
270
+
render json: { }, status::created
271
+
end
272
+
end
273
+
```
274
+
275
+
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.
276
+
277
+
### Prepending elements
278
+
279
+
_adding elements on the top of the list_
280
+
281
+
Prepending works pretty much the same as appending element, just configure your `cable` component like this:
Now imagine you want to update elements in your list without browser reload because somewhere else the title of a todo instance was changed. You could use `async` for this as well. Esspecially because `async` can react to serverside events pushed via ActionCable as well. But again: `async` would rerender the whole list... and in our usecase we do not want to this. We only want to update a specific element of the list. Luckily the implementation for this features does not differ from the above explained ones!
296
+
297
+
Imagine somewhere else the specific todo was updated via a form targeting the following controller action:
# respond to the form PUT request (needs to be last)
310
+
render json: { }, status::ok
311
+
end
312
+
end
313
+
```
314
+
315
+
Again, the controller action renders a new version of the component and pushes that to the clientside. Nothing changed here! We only need to tell the `cable` component to react properly to that event:
Please notice that it is mandatory to have a unique ID on the root element of each list item. The `cable` component will use the ID found in the root element of the pushed component in order to figure out, which element of the current list should be updated. In our example above we used `div id: "todo-#{todo.id}"` as the root element of our `todo_component` used for each element in the list.
326
+
327
+
### Removing elements
328
+
329
+
_removing existing elements within the list_
330
+
331
+
Well, of course we want to be able to remove elements from that list without rerendering the whole list, as `async` would do. The good thing: We can tell the `cable` component to delete elements by ID:
# respond to the DELETE request (needs to be last)
353
+
render json: { }, status::deleted
354
+
end
355
+
end
356
+
```
357
+
358
+
After deleting the todo instance, the controller action pushes an event via ActionCable, now including just the ID of the element which should be removed. Notice that this ID have to match the ID used on the root element of the component. In our example above we used `div id: "todo-#{todo.id}"` as the root element of our `todo_component` used for each element in the list.
359
+
360
+
### Replacing the whole component body
361
+
362
+
Now imagine a context in which the whole `cable` component body should be updated rather than just adding/updating/deleting specific elements of a list. In an online shop app this could be the shopping cart component rendered on the top right. When adding a product to the cart, you might want to update the shopping cart component in order to display the new amount of already included products.
363
+
364
+
The component may look like this:
365
+
366
+
```ruby
367
+
classShoppingCart < Matestack::Ui::Component
368
+
369
+
defresponse
370
+
div id:"shopping-cart", class: "some-fancy-styling"do
371
+
icon "some-shopping-cart-icon"
372
+
span count, class: "some-badge"
373
+
transition path: shopping_cart_path do
374
+
button "to my cart"
375
+
end
376
+
end
377
+
end
378
+
379
+
defcount
380
+
# some logic returning the amount of products in the users cart (saved on serverside)
381
+
current_user.cart.products_count
382
+
end
383
+
384
+
end
385
+
```
386
+
387
+
Imagine somewhere else the following controller action was targeted when adding a product to the cart:
388
+
389
+
```ruby
390
+
defadd_to_cart
391
+
@product=Product.find(params[:id])
392
+
current_user.cart.add(@product) #some logic adding the product to the users cart (saved on serverside)
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.:
0 commit comments