View Source Inertia.Controller (Inertia v0.4.0)

Controller functions for rendering Inertia.js responses.

Summary

Functions

Assigns a prop value to the Inertia page data.

Marks a prop value as "always included", which means it will be included in the props on initial page load and subsequent partial loads (even when it's not explicitly requested).

Marks a prop value as lazy, which means it will only get evaluated if explicitly requested in a partial reload.

Renders an Inertia response.

Types

@type always() :: {:keep, (... -> any())}
@type lazy() :: {:lazy, (... -> any())}

Functions

Link to this function

assign_prop(conn, key, value)

View Source
@spec assign_prop(Plug.Conn.t(), atom(), any()) :: Plug.Conn.t()

Assigns a prop value to the Inertia page data.

@spec inertia_always(value :: any()) :: always()

Marks a prop value as "always included", which means it will be included in the props on initial page load and subsequent partial loads (even when it's not explicitly requested).

@spec inertia_lazy(fun :: (... -> any())) :: lazy()

Marks a prop value as lazy, which means it will only get evaluated if explicitly requested in a partial reload.

Lazy props will only be included the when explicitly requested in a partial reload. If you want to include the prop on first visit, you'll want to use a bare anonymous function or named function reference instead.

conn
# ALWAYS included on first visit...
# OPTIONALLY included on partial reloads...
# ALWAYS evaluated...
|> assign_prop(:cheap_thing, cheap_thing())

# ALWAYS included on first visit...
# OPTIONALLY included on partial reloads...
# ONLY evaluated when needed...
|> assign_prop(:expensive_thing, fn -> calculate_thing() end)
|> assign_prop(:another_expensive_thing, &calculate_another_thing/0)

# NEVER included on first visit...
# OPTIONALLY included on partial reloads...
# ONLY evaluated when needed...
|> assign_prop(:super_expensive_thing, inertia_lazy(fn -> calculate_thing() end))
Link to this function

render_inertia(conn, component, props \\ %{})

View Source
@spec render_inertia(Plug.Conn.t(), component :: String.t(), props :: map()) ::
  Plug.Conn.t()

Renders an Inertia response.