Skip to content

Commit 652cdaa

Browse files
committed
added async defer docs
1 parent 5dd0bb7 commit 652cdaa

File tree

1 file changed

+86
-0
lines changed

1 file changed

+86
-0
lines changed

docs/components/async.md

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,73 @@ async hide_after: 1000 do
6464
end
6565
```
6666

67+
### Defer
68+
69+
The `defer` option may be used in two ways:
70+
71+
#### simple defer
72+
`defer: true` implies that the content of the `async` component gets requested within a separate GET request right after initial page load is done.
73+
```ruby
74+
async defer: true do
75+
div id: 'my-div' do
76+
plain 'I will be requested within a separate GET request right after initial page load is done'
77+
end
78+
end
79+
```
80+
81+
#### delayed defer
82+
`defer: 2000` means that the content of the `async` component gets requested within a separate GET request `2000ms` after initial page load is done.
83+
```ruby
84+
async defer: 2000 do
85+
div id: 'my-div' do
86+
plain 'I will be requested within a separate GET request 2000ms after initial page load is done'
87+
end
88+
end
89+
```
90+
91+
The content of an `async` component with activated `defer` behavior is not resolved within the first page load!
92+
93+
```ruby
94+
#...
95+
async defer: 1000 do
96+
some_database_data = SomeModel.some_heavy_query
97+
div id: 'my-div' do
98+
some_database_data.each do |some_instance|
99+
plain some_instance.id
100+
end
101+
end
102+
end
103+
async defer: 2000 do
104+
some_other_database_data = SomeModel.some_other_heavy_query
105+
div id: 'my-div' do
106+
some_other_database_data.each do |some_instance|
107+
plain some_instance.id
108+
end
109+
end
110+
end
111+
#...
112+
```
113+
114+
The `SomeModel.some_query` does not get executed within the first page load and only will be called within the deferred GET request. This helps us to render a complex UI with loads of heavy method calls step by step without slowing down the initial page load and rendering of simple content.
115+
116+
#### defer used with show_on and hide_on
117+
You can trigger the deferred GET request base on a client-side event fired by an `onclick` component for example.
118+
119+
```ruby
120+
onclick emit: "my_event" do
121+
button text: "show"
122+
end
123+
onclick emit: "my_other_event" do
124+
button text: "hide"
125+
end
126+
async defer: true, show_on: "my_event", hide_on: "my_other_event" do
127+
div id: 'my-div' do
128+
plain 'I will be requested within a separate GET request right after "my_event" is fired'
129+
end
130+
end
131+
```
132+
Everytime the `async` section gets shown, the content of the `async` component gets freshly fetched from the server!
133+
67134
## Examples
68135

69136
See some common use cases below:
@@ -183,3 +250,22 @@ page.execute_script('MatestackUiCore.matestackEventHub.$emit("my_event", { messa
183250
```
184251

185252
As a result, the event message gets shown _after_ our event was fired!
253+
254+
### Example 6: Deferred loading
255+
256+
On our example page, we wrap our async event around a placeholder for the event message.
257+
258+
```ruby
259+
class ExamplePage < Matestack::Ui::Page
260+
261+
def response
262+
components {
263+
async defer: true do
264+
div id: 'my-div' do
265+
plain 'I will be requested within a separate GET request right after initial page load is done'
266+
end
267+
end
268+
}
269+
end
270+
271+
end

0 commit comments

Comments
 (0)