Skip to content

Commit b3d8e50

Browse files
committed
Fix backend reference docs and example
1 parent f0eea03 commit b3d8e50

File tree

2 files changed

+33
-29
lines changed

2 files changed

+33
-29
lines changed

code/go/site/static/md/examples/animations.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,13 +67,14 @@ Building on the last example, we can fade in the new content the same way, start
6767
One of the nice features for reactivity is to show a spinner when a request is in flight. On any element that is using backend actions you can add a `data-indicator` attribute to show a spinner when the request is in flight. This can be done like so:
6868

6969
```html
70+
<div data-show="$fetching">Spinner</div>
7071
<button
71-
data-fetch-indicator="'#request_in_flight_indicator'"
72-
data-on-click="$post('/examples/animations/data/request_in_flight')"
7372
id="submit_request_in_flight"
73+
data-on-click="$post('/examples/animations/data/request_in_flight')"
74+
data-indicator="fetching"
7475
>
7576
Submit
7677
</button>
7778
```
7879

79-
This will show the element with the id `request_in_flight_indicator` when the request is in flight and hide it when the request is complete.
80+
This will show the spinner element when the request is in flight and hide it when the request is complete.

code/go/site/static/md/reference/plugins_backend.md

Lines changed: 29 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,6 @@
44

55
A set of plugins that allow for the integration of any backend service that supports SSE with Datastar.
66

7-
## Attribute Plugins
8-
9-
Request for data from the server via SSE and merge with the page.
10-
117
## Action Plugins
128

139
### `$get`, `$post`, `$put`, `$patch`, `$delete`
@@ -20,7 +16,24 @@ Makes an HTML_VERB request to the server and merges the response with the curren
2016

2117
Every request will be sent with a `{datastar: *}` object containing the current store (except for store keys beginning with an underscore). When using `$get` the store will be sent as a query parameter, otherwise it will be sent as a JSON body.
2218

23-
## Datastar SSE Event
19+
#### Options
20+
21+
The actions above take a second argument of options.
22+
23+
The `onlyRemoteSignals` option determines whether to only send remotely viewable store values (defaults to `true`).
24+
25+
The `headers` option is an object containing headers to send with the request.
26+
27+
```html
28+
<div data-on-click="$get('/examples/click_to_edit/contact/1', {
29+
onlyRemoteSignals: false,
30+
headers: {
31+
'X-Csrf-Token': 'JImikTbsoCYQ9oGOcvugov0Awc5LbqFsZW6ObRCxuqFHDdPbuFyc4ksPVVa9+EB4Ag+VU6rpc680edNFswIRwg==',
32+
},
33+
})"></div>
34+
```
35+
36+
### Datastar SSE Events
2437

2538
An example of a minimal valid response would be:
2639

@@ -40,7 +53,7 @@ Additional `data` lines can be added to the response to override the default beh
4053
</p>
4154
</div>
4255

43-
### datastar-merge-fragments
56+
#### `datastar-merge-fragments`
4457

4558
| Key | Description |
4659
|------------------------------------|-------------------------------------------------------------------------------------------------------------------------|
@@ -57,7 +70,7 @@ Additional `data` lines can be added to the response to override the default beh
5770
| `data: useViewTransition true` | Whether to use view transitions when merging into the DOM. Defaults to `false`. |
5871
| `data: fragments` | The HTML fragments to merge into the DOM. |
5972

60-
### datastar-merge-signals
73+
#### `datastar-merge-signals`
6174

6275
```go
6376
event: datastar-merge-signals
@@ -67,7 +80,7 @@ data: signals {foo: 1234}
6780

6881
The `datastar-merge-signals` event is used to update the store with new values. The `onlyIfMissing` line determines whether to update the store with new values only if the key does not exist. The `signals` line should be a valid `data-store` attribute. This will get merged into the store.
6982

70-
### datastar-remove-fragments
83+
#### `datastar-remove-fragments`
7184

7285
```go
7386
event: datastar-remove-fragments
@@ -83,7 +96,7 @@ data: paths foo.bar 1234 abc
8396

8497
The `datastar-remove-signals` event is used to remove signals that match the provided paths from the store.
8598

86-
### datastar-execute-script
99+
#### `datastar-execute-script`
87100

88101
```go
89102
event: datastar-execute-script
@@ -97,22 +110,22 @@ The `datastar-execute-script` event is used to execute JavaScript in the browser
97110

98111
## Attribute Plugins
99112

100-
### Fetch Indicator
113+
### `data-indicator`
101114

102115
```html
103-
<svg id="foo">Spinner</svg>
116+
<svg data-show="$fetching">Spinner</svg>
104117
<button
105118
data-on-click="$get('/examples/click_to_edit/contact/1')"
106-
data-fetch-indicator="#foo"
107-
data-bind-disabled="$isFetching('#foo')"
119+
data-indicator="fetching"
120+
data-bind-disabled="$fetching"
108121
></button>
109122
```
110123

111-
Show a spinner when the request is in flight. The `data-fetch-indicator` attribute should be a CSS selector to the element(s). When the attribute is present, the element will be hidden when requests are not in flight and shown when they are.
124+
The `data-indicator` attribute sets the value of the provided signal name to `true` while the request is in flight. This signal can be used by other attributes to show a loading spinner, disable a button, etc.
112125

113-
The `$isFetching` action returns a computed value that allows you to easily react to the state of the indicator.
126+
Note that elements using the `data-indicator` attribute ***must*** have a unique ID attribute.
114127

115-
### Replace URL
128+
### `data-replace-url`
116129

117130
```html
118131
<div
@@ -127,13 +140,3 @@ Replaces the URL in the browser without reloading the page. The value can be a r
127140
data-replace-url="`/page{$page}`">
128141
</div>
129142
```
130-
131-
### Headers
132-
133-
```html
134-
<div
135-
data-header="{'x-csrf-token':'JImikTbsoCYQ9oGOcvugov0Awc5LbqFsZW6ObRCxuqFHDdPbuFyc4ksPVVa9+EB4Ag+VU6rpc680edNFswIRwg=='}">
136-
</div>
137-
```
138-
139-
Can be added anywhere on the page and will be included on SSE fetches. In general, you should lean to Cookies unless your backend framework demand it.

0 commit comments

Comments
 (0)