StarView. StarView behaviour
(star_view v0.3.2)
Copy Markdown
Behaviour for StarView-enabled Phoenix controllers.
Use use StarView, :controller in your AppWeb.controller/0 macro, then
implement callbacks with @impl StarView:
Lifecycle
mount/2— Sets up initial signals and assigns for the page load.render/1— Renders the HEEx template. Useinit_signals/1to emit thedata-signalsattribute for the initial client state.handle_event/3— Called byStarView.Phoenix.Dispatchwhen a Datastar action fires. The dispatcher starts the SSE response before this callback and flushes tracked signals afterwards.
Example
@impl StarView
def mount(conn, _params) do
conn
|> signal(:count, 0)
|> signal(:step, 1)
end
@impl StarView
def render(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("increment", signals, conn) do
conn
|> signal(:count, Map.get(signals, "count", 0) + 1)
end
Summary
Callbacks
@callback handle_event(String.t(), map(), Plug.Conn.t()) :: Plug.Conn.t()
@callback mount(Plug.Conn.t(), map()) :: Plug.Conn.t()