Actions

Datastar provides actions (helper functions) that can be used in Datastar expressions.

The @ prefix designates Datastar actions in expressions. Datastar rewrites action calls and dispatches them to registered action functions. It does not prevent other JavaScript from being executed in an expression.

@peek() #

@peek(callable: () => any)

Allows accessing signals without subscribing to their changes in expressions.

1<div data-text="$foo + @peek(() => $bar)"></div>

In the example above, the expression in the data-text attribute will be re-evaluated whenever $foo changes, but it will not be re-evaluated when $bar changes, since it is evaluated inside the @peek() action.

@setAll() #

@setAll(value: any, filter?: {include: RegExp, exclude?: RegExp})

Sets the value of all matching signals (or all signals if no filter is used) to the expression provided in the first argument. The second argument is an optional filter object with an include property that accepts a regular expression to match signal paths. You can optionally provide an exclude property to exclude specific patterns.

The Datastar Inspector can be used to inspect and filter current signals and view signal patch events in real-time.
 1<!-- Sets the `foo` signal only -->
 2<div data-signals:foo="false">
 3    <button data-on:click="@setAll(true, {include: /^foo$/})"></button>
 4</div>
 5
 6<!-- Sets all signals starting with `user.` -->
 7<div data-signals="{user: {name: '', nickname: ''}}">
 8    <button data-on:click="@setAll('johnny', {include: /^user\./})"></button>
 9</div>
10
11<!-- Sets all signals except those ending with `_temp` -->
12<div data-signals="{data: '', data_temp: '', info: '', info_temp: ''}">
13    <button data-on:click="@setAll('reset', {include: /.*/, exclude: /_temp$/})"></button>
14</div>

@toggleAll() #

@toggleAll(filter?: {include: RegExp, exclude?: RegExp})

Toggles the boolean value of all matching signals (or all signals if no filter is used). The argument is an optional filter object with an include property that accepts a regular expression to match signal paths. You can optionally provide an exclude property to exclude specific patterns.

The Datastar Inspector can be used to inspect and filter current signals and view signal patch events in real-time.
 1<!-- Toggles the `foo` signal only -->
 2<div data-signals:foo="false">
 3    <button data-on:click="@toggleAll({include: /^foo$/})"></button>
 4</div>
 5
 6<!-- Toggles all signals starting with `is` -->
 7<div data-signals="{isOpen: false, isActive: true, isEnabled: false}">
 8    <button data-on:click="@toggleAll({include: /^is/})"></button>
 9</div>
10
11<!-- Toggles signals starting with `settings.` -->
12<div data-signals="{settings: {darkMode: false, autoSave: true}}">
13    <button data-on:click="@toggleAll({include: /^settings\./})"></button>
14</div>

Backend Actions #

@get() #

@get(uri: string, options={ })

Sends a GET request to the backend using the Fetch API. The URI can be any valid endpoint, and the response type any of the allowed response types, or a 204 No Content response if the response body is empty.

1<button data-on:click="@get('/endpoint')"></button>

By default, requests are sent with a Datastar-Request: true header, and a {datastar: *} object containing all existing signals, except those beginning with an underscore. This behavior can be changed using the filterSignals option, which allows you to include or exclude specific signals using regular expressions.

When using a get request, the signals are sent as a query parameter, otherwise they are sent as a JSON body.

When a page is hidden (in a background tab, for example), the default behavior for get requests is for the SSE connection to be closed, and reopened when the page becomes visible again. To keep the connection open when the page is hidden, set the openWhenHidden option to true.

1<button data-on:click="@get('/endpoint', {openWhenHidden: true})"></button>

It’s possible to send form encoded requests by setting the contentType option to form. This sends requests using application/x-www-form-urlencoded encoding.

1<button data-on:click="@get('/endpoint', {contentType: 'form'})"></button>

It’s also possible to send requests using multipart/form-data encoding by specifying it in the form element’s enctype attribute. This should be used when uploading files. See the form data example.

1<form enctype="multipart/form-data">
2    <input type="file" name="file" />
3    <button data-on:click="@post('/endpoint', {contentType: 'form'})"></button>
4</form>

@post() #

@post(uri: string, options={ })

Works the same as @get() but sends a POST request to the backend.

1<button data-on:click="@post('/endpoint')"></button>

@put() #

@put(uri: string, options={ })

Works the same as @get() but sends a PUT request to the backend.

1<button data-on:click="@put('/endpoint')"></button>

@patch() #

@patch(uri: string, options={ })

Works the same as @get() but sends a PATCH request to the backend.

1<button data-on:click="@patch('/endpoint')"></button>

@query() #

@query(uri: string, options={ })

Works the same as @get() but sends a QUERY request to the backend. Unlike GET, signals are sent in the request body, allowing large or complex queries without encoding them in the URI. Unlike POST, QUERY is a safe and idempotent HTTP method intended for read-only operations.

1<button data-on:click="@query('/endpoint')"></button>

@delete() #

@delete(uri: string, options={ })

Works the same as @get() but sends a