OctaStar. StarView behaviour
(octa_star v0.1.0)
Copy Markdown
Behaviour for OctaStar-enabled Phoenix controllers.
Use use OctaStar, :controller in your AppWeb.controller/0 macro, then
implement callbacks with @impl StarView:
Lifecycle
show/2— Sets up initial signals and assigns for the page load.html/1— Renders the HEEx template. Useinit_signals/1to emit thedata-signalsattribute for the initial client state.handle_event/3— Called byOctaStar.Phoenix.Dispatchwhen a Datastar action fires. The dispatcher starts the SSE response before this callback and flushes tracked signals afterwards.
Example
@impl StarView
def show(conn, _params) do
conn
|> signal(:count, 0)
|> signal(:step, 1)
end
@impl StarView
def html(assigns) do
~H"""
<div data-signals={init_signals(@conn)}>
<button data-on:click={post("increment")}>+</button>
<span data-text="$count">{@count}</span>
</div>
"""
end
@impl StarView
def handle_event(conn, "increment", signals) do
signal(conn, :count, Map.get(signals, "count", 0) + 1)
end
Summary
Callbacks
@callback handle_event(Plug.Conn.t(), String.t(), map()) :: Plug.Conn.t()
@callback show(Plug.Conn.t(), map()) :: Plug.Conn.t()