Dstar.Page.Assigns (dstar v0.1.0-alpha.2)

Copy Markdown View Source

Assign helpers that work in both halves of a Dstar page module.

use Dstar.Page imports these instead of Phoenix.Component's assign family, so one set of names works everywhere:

  • On a %Plug.Conn{} (in mount/2, handle_event/3, handle_connect/2, handle_info/2) they behave like Plug.Conn.assign/3.
  • On anything else (sockets, assigns maps inside function components) they delegate to Phoenix.Component.

Summary

Functions

Assigns one key/value or many key/values.

Assigns a value computed by fun only when key is absent. fun may take zero arguments or the current assigns.

Updates an existing assign with fun. Raises KeyError if absent.

Functions

assign(conn, key_values)

Assigns one key/value or many key/values.

conn |> assign(:count, 1)
conn |> assign(count: 1, name: "rico")

Keys must be atoms when assigning to a %Plug.Conn{}, matching Plug.Conn.assign/3 semantics.

assign(conn, key, value)

assign_new(conn, key, fun)

Assigns a value computed by fun only when key is absent. fun may take zero arguments or the current assigns.

update(conn, key, fun)

Updates an existing assign with fun. Raises KeyError if absent.