Caravela.Live.Template (Caravela v0.8.0)

Copy Markdown View Source

use macro that binds a Phoenix LiveView to a Caravela.Live.Domain module.

Injects a default mount/3 (assigning the domain's initial state) and a default handle_event/3 that dispatches to the domain's on_event handlers. Also imports apply_updater/2,3, sugar around Caravela.Live.Updater.apply/2,3 that looks up updaters by name on the bound domain.

defmodule MyAppWeb.BookEditorLive do
  use MyAppWeb, :live_view
  use Caravela.Live.Template, domain: MyApp.BookEditorDomain

  def render(assigns) do
    ~H"""
    <LiveSvelte.svelte
      name="library/BookEditor"
      props={%{book: @book, saving: @saving}}
      socket={@socket}
    />
    """
  end
end

The Svelte component's pushEvent calls arrive as LiveView events and are routed through the domain's on_event handlers. If no handler matches, the default handle_event/3 lets the LiveView's own (developer-written) clauses take over via Elixir's normal clause-matching — this macro only adds a catch-all at the bottom.

Summary

Functions

Apply a named updater to the socket. The name is resolved against the Caravela.Live.Domain module bound at use time.

Apply a named 2-arity updater with an event-payload argument.

Functions

apply_updater(socket, updater_name)

(macro)

Apply a named updater to the socket. The name is resolved against the Caravela.Live.Domain module bound at use time.

def handle_event("save", _params, socket) do
  {:noreply, socket |> apply_updater(:mark_saving)}
end

Raises if the name isn't registered on the domain.

apply_updater(socket, updater_name, arg)

(macro)

Apply a named 2-arity updater with an event-payload argument.

socket |> apply_updater(:set_book, book)