Skip to content

Commit 054803a

Browse files
author
Nils Henning
committed
[TASK] add async component guide
1 parent 2a9992e commit 054803a

File tree

3 files changed

+65
-1
lines changed

3 files changed

+65
-1
lines changed

docs/guides/400-transitions/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,5 +45,6 @@ end
4545
Styling a transition which is _active_ is simple, because it automatically gets the `active` class on the clientside when the current path equals it's target path. When a sub page of a parent `transition` component is currently active, the parent `transition` component gets a `active-child` class.
4646

4747

48+
## Complete documentation
4849

4950
If you want to know all details about the `transition` component, like how you can delay it or what events it emits, checkout it's [api documentation](/docs/api/100-components/transition.md)

docs/guides/500-forms/README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ To learn more about the details of each input component take a look at the [form
9292

9393
Wrap a button or any markup which should submit the form when clicked in `form_submit`.
9494

95-
----
95+
96+
## Complete documentation
9697

9798
If you want to know all details about the `form` component and all inputs and their usage as well as how you can customize errors, input placeholder, input value initialization and more checkout it's [api documentation](/docs/api/100-components/form.md).

docs/guides/700-async/README.md

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,65 @@
11
# Async
22

3+
Use the `async` component to load or reload content asynchronously depending on events or page initialization. An `async` component can be used to either defer some content on page loads or to rerender a component. For example deferring complex components to increase the initial page load time and render this components asynchronously afterwards. Or rerendering a table after a "reload" button click or rerendering a chat view through action cable events.
4+
5+
The `async` component can be used to load or reload content asynchronously depending on page initialization or events. Simply wrap your content which you want to render asynchronously inside the `async` component. In order to load the content asynchronously after the initial page load use `defer: true` or pass in a number to delay the defer. To reload content you can use `:rerender_on` with an event name, leveraging the event hub, to reload the content if the specified event occurs. For example rerendering a list of todos beneath the form to create todos to instantly show new created objects. Remember events can also be used with action cable, which you could use for "real time" synchronization.
6+
7+
## Usage
8+
9+
The `async` component is very straightforward in its usage. Every `async` component needs an `:id`, which is necessary for resolving the `async` component and rendering the content in the correct place afterwards. So make sure your ids for async components on the same page are unique. Specifying one of `:defer` or `:rerender_on` or both enables deferred loading and/or rerendering on events. If `:defer` is not configured or false the `async` component will be rendered on page load.
10+
11+
### Deferring content
12+
13+
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.
14+
15+
```ruby
16+
def response
17+
async id: 'deferred-async', defer: true do
18+
plain 'Some content rendered after page is loaded.'
19+
end
20+
end
21+
```
22+
23+
The above `async` component will be rendered asynchronously after page load.
24+
25+
```ruby
26+
def response
27+
async id: 'delayed-deferred-async', defer: 500 do
28+
plain 'Some delayed deferred content'
29+
end
30+
end
31+
```
32+
33+
Specifying `defer: 500` will delay the asynchronous request after page load of the `async` component for 300ms and render the content afterwards.
34+
35+
### Rerendering content
36+
37+
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.
38+
39+
```ruby
40+
def response
41+
async id: 'rerendering-async', rerender_on: 'update-time' do
42+
paragraph text: Time.now
43+
end
44+
onclick emit: 'update-time' do
45+
button text: 'Update time'
46+
end
47+
end
48+
```
49+
50+
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.
51+
52+
### Loading animations
53+
54+
An `async` components wraps its content in a special html structure allowing you to create animations while the components gets loaded or rerendered. It appends a loading class to the wrapping elements while the component is loading or rerendering. To learn more about how to animate loading `async` components checkout its [api documentation](/docs/api/100-components/async.md).
55+
56+
57+
## Complete documentation
58+
59+
If you want to know all details about the `async` component checkout it's [api documentation](/docs/api/100-components/async.md)
60+
61+
62+
63+
64+
365
![Coming Soon](../../images/coming_soon.png)

0 commit comments

Comments
 (0)