LiveView¶
Server-side view that manages state and renders HTML.
Implement this trait to define a LiveView:
mountinitializes assigns on the socket when the connection is established.handle_eventresponds to client interactions (clicks, form submits) by updating assigns on the socket.renderproduces HTML from the current assigns. This is a pure function of data — theboxreceiver prevents mutation during rendering.
Public Functions¶
mount¶
Called when a connection is established or when the view is rendered
for HTTP response via PageRenderer. Use socket.assign() to set
initial state.
Check socket.connected() to distinguish WebSocket mount (true) from
HTTP render (false) — for example, to skip subscribing to PubSub topics
during HTTP render.
Parameters¶
- socket: Socket ref
Returns¶
- None val
handle_event¶
Called when the client sends a UI event (e.g., a button click or form
change/submission). Use
socket.assign() to update state — the framework re-renders
automatically if any assigns changed.
fun ref handle_event(
event: String val,
payload: (JsonObject val | JsonArray val | String val |
I64 val | F64 val | Bool val |
None val),
socket: Socket ref)
: None val
Parameters¶
- event: String val
- payload: (JsonObject val | JsonArray val | String val | I64 val | F64 val | Bool val | None val)
- socket: Socket ref
Returns¶
- None val
handle_info¶
Called when an external actor sends a message to this connection
via PubSub or direct InfoReceiver.info calls.
Default implementation does nothing. Override to handle server-push messages from timers, background jobs, or PubSub topics.
Parameters¶
Returns¶
- None val
render¶
Return the full HTML for the current state. Partial because rendering may fail (e.g., missing template variable). On failure the framework keeps the last successfully rendered HTML and sends an error to the client.
The returned HTML must have a single root element (e.g., wrapped in a
<div>) for morphdom to work correctly during DOM patching.
Parameters¶
- assigns: Assigns box
Returns¶
- String val ?
render_parts¶
Drive the given sink with static/dynamic template output for efficient wire updates.
When this returns true, the framework sends static template parts once
per connection and only changed dynamic slot values on subsequent
renders. When it returns false (the default), the framework falls back
to render() and sends full HTML every time.
Override this to enable split rendering. Typical implementation:
fun box render_parts(assigns: Assigns box,
sink: templates.TemplateSink ref): Bool
=>
try
_template.render_to(sink, assigns.template_values())?
true
else
false
end
Parameters¶
- assigns: Assigns box
- sink: TemplateSink ref
Returns¶
- Bool val