This repository was archived by the owner on Jul 19, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 46
Added explanation of the fetch module. #57
Open
teymour-aldridge
wants to merge
16
commits into
yewstack:master
Choose a base branch
from
teymour-aldridge:explain-fetch-module
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
4d04169
Added explanation of the fetch module.
teymour-aldridge 4b53d40
Add further reading.
teymour-aldridge 4ad3a66
Added example closure for error handling.
teymour-aldridge 55a2d8e
Added functional example.
teymour-aldridge d9ae58d
Create `FetchService` on the fly.
teymour-aldridge 38c39bd
Remove unnecessary `fetch_service` field.
teymour-aldridge 6a56bf8
Remove redundant `fetch_service` field.
teymour-aldridge 9e454be
Update src/concepts/services/fetch.md
teymour-aldridge cb60e6d
Update src/concepts/services/fetch.md
teymour-aldridge 43a6755
Break out logic from `view` function.
teymour-aldridge 5f4d130
Update fetch.md
teymour-aldridge aa5d513
Add missing `match` expression in example.
teymour-aldridge af9d7be
Fixed a type error.
teymour-aldridge 4aabb63
Add some error handling details.
teymour-aldridge 3419f7c
Shorten line lengths (to ~100 characters max per line).
teymour-aldridge 8c42997
Make a sentence more detailed.
teymour-aldridge File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| # Fetch | ||
| ## Introduction | ||
| The format module can be used to make requests to a server. This allows a Yew application to connect to a backend. | ||
|
|
||
| ## Making requests | ||
| ### Building requests | ||
| Before you can send a request to the server, you need to construct it. This should be done using the `yew::services::fetch::Request` object. | ||
| ```rust | ||
| use yew::services::fetch::Request; | ||
| use yew::format::Nothing; | ||
| let get_request = Request::get("https://example.com/api/v1/something").body(Nothing).expect("Could not build that request") | ||
| ``` | ||
|
|
||
| ```rust | ||
| use yew::services::fetch::Request; | ||
| use yew::format::Json; | ||
| use serde_json::json; | ||
| let post_request = Request::post("https://example.com/api/v1/post/something").header("Content-Type", "application/json").body(Json(&json!({"key": "value"}))).expect("Could not build that request.") | ||
| ``` | ||
| ### Dispatching requests | ||
| To dispatch requests an instance of the `fetch_service` is needed. This can be created using `yew::services::FetchService::new()`. After building a request, it can be dispatched using `FetchService`'s `fetch` method. This method takes two arguments: a request and a `ComponentLink` callback with a closure taking an instance of `yew::services::fetch::Response`. This closure is run after the request finishes and can be used to initiatiate a redraw of the component by returning a message. | ||
|
|
||
jstarry marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| ## Further reading | ||
jstarry marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| * [The API documentation](https://docs.rs/yew/0.14.3/yew/services/fetch/index.html) | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.