Dstar.Actions (dstar v0.1.0-alpha.2)

Copy Markdown View Source

Helpers for generating Datastar action expressions and encoding module names.

Examples

# In a Phoenix template:
<button data-on:click={Dstar.post(MyApp.CounterHandler, "increment")}>+</button>

# Other HTTP verbs:
<button data-on:click={Dstar.delete(MyApp.ItemHandler, "remove")}>×</button>

# Dynamic module (reads from signal):
<button data-on:click={Dstar.post("increment")}>+</button>

Summary

Functions

Decodes a URL-safe module name back to an Elixir module.

Generates a @delete(...) action expression for Datastar attributes.

Generates a @delete(...) expression with a URL prefix.

Encodes a module name for URL use.

Generates a @get(...) action expression for Datastar attributes.

Generates a @get(...) expression with a URL prefix.

Generates a @patch(...) action expression for Datastar attributes.

Generates a @patch(...) expression with a URL prefix.

Generates a @post(...) action expression for Datastar attributes.

Generates a @post(...) expression with a URL prefix.

Generates a @put(...) action expression for Datastar attributes.

Generates a @put(...) expression with a URL prefix.

Functions

decode_module(encoded)

@spec decode_module(String.t()) :: {:ok, module()} | :error

Decodes a URL-safe module name back to an Elixir module.

Returns {:ok, module} if the module exists, :error otherwise.

Examples

iex> Dstar.Actions.decode_module("my_app-counter_view")
{:ok, MyApp.CounterView}

delete(module_or_name, name_or_opts \\ [])

@spec delete(module(), String.t()) :: String.t()
@spec delete(
  String.t(),
  keyword()
) :: String.t()

Generates a @delete(...) action expression for Datastar attributes.

CSRF is handled automatically — Datastar sends all signals (including csrf) as body params, and RenameCsrfParam maps it to _csrf_token.

With a known module (compile-time):

iex> Dstar.Actions.delete(MyApp.CounterHandler, "increment")
"@delete('/ds/my_app-counter_handler/increment')"

With a dynamic module signal (runtime on client):

iex> Dstar.Actions.delete("increment")
"@delete('/ds/' + $_dstar_module + '/increment')"

With a URL prefix:

iex> Dstar.Actions.delete(MyApp.Handler, "save", prefix: "/ws")
"@delete('/ws/ds/my_app-handler/save')"

Options

  • :prefix — URL path prefix (e.g. "/my-workspace"). Only for the module form.
  • :module — Override the module signal name (default: $_dstar_module). Only for the dynamic form.

delete(module, event_name, opts)

@spec delete(module(), String.t(), keyword()) :: String.t()

Generates a @delete(...) expression with a URL prefix.

Example

iex> Dstar.Actions.delete(MyApp.Handler, "save", prefix: "/my-workspace")
"@delete('/my-workspace/ds/my_app-handler/save')"

encode_module(module)

@spec encode_module(module()) :: String.t()

Encodes a module name for URL use.

Examples

iex> Dstar.Actions.encode_module(MyApp.CounterView)
"my_app-counter_view"

iex> Dstar.Actions.encode_module(MyApp.Web.ChatView)
"my_app-web-chat_view"

event(module_or_name, name_or_opts \\ [])

This function is deprecated. Use Dstar.Actions.post/2 (or get/put/patch/delete) instead.

event(module, event_name, opts)

This function is deprecated. Use Dstar.Actions.post/3 (or get/put/patch/delete) instead.

get(module_or_name, name_or_opts \\ [])

@spec get(module(), String.t()) :: String.t()
@spec get(
  String.t(),
  keyword()
) :: String.t()

Generates a @get(...) action expression for Datastar attributes.

CSRF is handled automatically — Datastar sends all signals (including csrf) as body params, and RenameCsrfParam maps it to _csrf_token.

With a known module (compile-time):

iex> Dstar.Actions.get(MyApp.CounterHandler, "increment")
"@get('/ds/my_app-counter_handler/increment')"

With a dynamic module signal (runtime on client):

iex> Dstar.Actions.get("increment")
"@get('/ds/' + $_dstar_module + '/increment')"

With a URL prefix:

iex> Dstar.Actions.get(MyApp.Handler, "save", prefix: "/ws")
"@get('/ws/ds/my_app-handler/save')"

Options

  • :prefix — URL path prefix (e.g. "/my-workspace"). Only for the module form.
  • :module — Override the module signal name (default: $_dstar_module). Only for the dynamic form.

get(module, event_name, opts)

@spec get(module(), String.t(), keyword()) :: String.t()

Generates a @get(...) expression with a URL prefix.

Example

iex> Dstar.Actions.get(MyApp.Handler, "save", prefix: "/my-workspace")
"@get('/my-workspace/ds/my_app-handler/save')"

patch(module_or_name, name_or_opts \\ [])

@spec patch(module(), String.t()) :: String.t()
@spec patch(
  String.t(),
  keyword()
) :: String.t()

Generates a @patch(...) action expression for Datastar attributes.

CSRF is handled automatically — Datastar sends all signals (including csrf) as body params, and RenameCsrfParam maps it to _csrf_token.

With a known module (compile-time):

iex> Dstar.Actions.patch(MyApp.CounterHandler, "increment")
"@patch('/ds/my_app-counter_handler/increment')"

With a dynamic module signal (runtime on client):

iex> Dstar.Actions.patch("increment")
"@patch('/ds/' + $_dstar_module + '/increment')"

With a URL prefix:

iex> Dstar.Actions.patch(MyApp.Handler, "save", prefix: "/ws")
"@patch('/ws/ds/my_app-handler/save')"

Options

  • :prefix — URL path prefix (e.g. "/my-workspace"). Only for the module form.
  • :module — Override the module signal name (default: $_dstar_module). Only for the dynamic form.

patch(module, event_name, opts)

@spec patch(module(), String.t(), keyword()) :: String.t()

Generates a @patch(...) expression with a URL prefix.

Example

iex> Dstar.Actions.patch(MyApp.Handler, "save", prefix: "/my-workspace")
"@patch('/my-workspace/ds/my_app-handler/save')"

post(module_or_name, name_or_opts \\ [])

@spec post(module(), String.t()) :: String.t()
@spec post(
  String.t(),
  keyword()
) :: String.t()

Generates a @post(...) action expression for Datastar attributes.

CSRF is handled automatically — Datastar sends all signals (including csrf) as body params, and RenameCsrfParam maps it to _csrf_token.

With a known module (compile-time):

iex> Dstar.Actions.post(MyApp.CounterHandler, "increment")
"@post('/ds/my_app-counter_handler/increment')"

With a dynamic module signal (runtime on client):

iex> Dstar.Actions.post("increment")
"@post('/ds/' + $_dstar_module + '/increment')"

With a URL prefix:

iex> Dstar.Actions.post(MyApp.Handler, "save", prefix: "/ws")
"@post('/ws/ds/my_app-handler/save')"

Options

  • :prefix — URL path prefix (e.g. "/my-workspace"). Only for the module form.
  • :module — Override the module signal name (default: $_dstar_module). Only for the dynamic form.

post(module, event_name, opts)

@spec post(module(), String.t(), keyword()) :: String.t()

Generates a @post(...) expression with a URL prefix.

Example

iex> Dstar.Actions.post(MyApp.Handler, "save", prefix: "/my-workspace")
"@post('/my-workspace/ds/my_app-handler/save')"

put(module_or_name, name_or_opts \\ [])

@spec put(module(), String.t()) :: String.t()
@spec put(
  String.t(),
  keyword()
) :: String.t()

Generates a @put(...) action expression for Datastar attributes.

CSRF is handled automatically — Datastar sends all signals (including csrf) as body params, and RenameCsrfParam maps it to _csrf_token.

With a known module (compile-time):

iex> Dstar.Actions.put(MyApp.CounterHandler, "increment")
"@put('/ds/my_app-counter_handler/increment')"

With a dynamic module signal (runtime on client):

iex> Dstar.Actions.put("increment")
"@put('/ds/' + $_dstar_module + '/increment')"

With a URL prefix:

iex> Dstar.Actions.put(MyApp.Handler, "save", prefix: "/ws")
"@put('/ws/ds/my_app-handler/save')"

Options

  • :prefix — URL path prefix (e.g. "/my-workspace"). Only for the module form.
  • :module — Override the module signal name (default: $_dstar_module). Only for the dynamic form.

put(module, event_name, opts)

@spec put(module(), String.t(), keyword()) :: String.t()

Generates a @put(...) expression with a URL prefix.

Example

iex> Dstar.Actions.put(MyApp.Handler, "save", prefix: "/my-workspace")
"@put('/my-workspace/ds/my_app-handler/save')"