Wondering if anyone has already crossed this bridge or can point to an example app that has done this?
This is from the LiveView docs
Referencing parent assigns
When a LiveView is mounted in a disconnected state, the Plug.Conn assigns will be available for reference via
assign_new/3
, allowing assigns to be shared for the initial HTTP request. On connected mount, theassign_new/3
would be invoked, and the LiveView would use its session to rebuild the originally shared assign. Likewise, nested LiveView children have access to their parent’s assigns on mount usingassign_new
, which allows assigns to be shared down the nested LiveView tree.
# controller
conn
|> assign(:current_user, user)
|> LiveView.Controller.live_render(MyLive, session: %{user_id: user.id})
# LiveView mount
def mount(%{user_id: user_id}, socket) do
{:ok, assign_new(socket, :current_user, fn -> Accounts.get_user!(user_id) end)}
end
Doesn’t really say a lot about how to do it or what would be best practice, just that it’s possible
I’m curious, are the nested LiveViews rendered from inside the template using live_render(@conn, MyappWeb.NameofviewLive)
?
Will a user have a different socket for each LiveView on the page?
Imagine an app that has a bunch of different “widget” blocks on the page that are all their own LiveViews, you can swap in and out different widgets like a calendar widget, weather widget, chat widget, etc. Is this even possible?
You could also use nested LiveViews for 1 on 1 private chats like on Facebook, where every chat is it’s own LiveView. Is this even a good idea?
Just brainstorming and throwing out ideas here, interested to know if anyone has played around with nesting.