Skip to content

Commit 4c5455a

Browse files
authored
Add simple example for hooks (#40)
* Add simple example for hooks * Fix withProgress function example
1 parent 6698d24 commit 4c5455a

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

README.md

+33
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,39 @@ RequestInterceptor.register(async (request) => {
129129
RequestInterceptor.reset()
130130
```
131131

132+
#### Before and after hooks
133+
134+
Wrap the request `Promise` with your own code. Just pure and simple JavaScript like this:
135+
136+
```javascript
137+
import { FetchRequest } from "@rails/request.js"
138+
import { navigator } from "@hotwired/turbo"
139+
140+
function showProgressBar() {
141+
navigator.delegate.adapter.progressBar.setValue(0)
142+
navigator.delegate.adapter.progressBar.show()
143+
}
144+
145+
function hideProgressBar() {
146+
navigator.delegate.adapter.progressBar.setValue(1)
147+
navigator.delegate.adapter.progressBar.hide()
148+
}
149+
150+
export function withProgress(request) {
151+
showProgressBar()
152+
153+
return request.then((response) => {
154+
hideProgressBar()
155+
return response
156+
})
157+
}
158+
159+
export function get(url, options) {
160+
const request = new FetchRequest("get", url, options)
161+
return withProgress(request.perform())
162+
}
163+
```
164+
132165
## Response
133166

134167
### statusCode

0 commit comments

Comments
 (0)