sbaildon
Dispatching LiveComponent based on module
I have a LiveView where I want to render two different live components that each have their own representation of the same module. Think something like Elixir forum’s editor window: a panel with controls on the left, and the formatted output on the right. Both operate on the same content, but each render differently.
I thought about using protocols, something like
# Dashboard.ex
defmodule Dashboard do
defprotocol Editor do
def render(struct)
end
defimpl Editor, for: Avatar do
use Phoenix.LiveComponent
def render(struct) do
~H"""
<div phx-target={@myself}>...</div>
"""
end
def handle_event("event", _, socket) do
{:noreply, socket}
end
end
defprotocol Preview do
def render(struct)
end
defimpl Preview, for: Avatar do
use Phoenix.LiveComponent
def render(struct) do
~H"""
<div phx-target={@myself}>...</div>
"""
end
def handle_event("event", _, socket) do
{:noreply, socket}
end
end
end
# view.heex
<%= live_component Dashboard.Editor, @avatar %>
<%= live_component Dashboard.Preview, @avatar %>
Because live_component passes assigns as an enumerable, I’m not sure I can get the behaviour I want here. Does anyone have any suggestions? Should I just stick with pattern matching?
First Post!
APB9785
Creator of ECSx
This should work just the fine the way you are describing. But when you pass the assigns to a LiveComponent, it should be a keyword list like this:
<%= live_component Dashboard.Editor,
id: "editor-component",
avatar: @avatar,
content: @content %>
<%= live_component Dashboard.Preview,
id: "preview-component",
avatar: @avatar,
content: @content %>
Popular in Questions
What is the proper way to load a module from a file in to IEX?
In the python world, doing something like this pretty standard:
from ....
New
Good day to you all.
I have been struggling to get a query involving like and ilike to work.
Can anyone assist me on this, please?
pro...
New
Hi all,
Trying to get some more clarity over utc_datetime and naive_datetime for Ecto:
https://hexdocs.pm/ecto/Ecto.Schema.html#module-...
New
i’m a new one to elixir
which editor can i use
vs code? or atom?
Thanks! :smiley:
New
I’m working on defining a simple Ecto schema for a table (in PostGres), but I don’t see where I can define a column as NOT NULL. Conside...
New
If I have a string “1000 cfu/ml” . I want to remove the characters and / and space . So the string is like this
"1000"
What is the ...
New
The Elixir Typespec docs show the following syntax for keyword lists in typespecs:
# ...
| [key: type] # keyword lis...
New
Hello, I have map which I want to convert it to string like this:
the map:
%{last_name: "tavakkoli", name: "shahryar"}
the string I ne...
New
Hi guys, i’m new in the Elixir world, and i have to say, that i love it!
i’m having some problem to understand anonymous functions with ...
New
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New
Other popular topics
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
New
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1]
15:22:35.803 [error] gen_event {lager_file_backend...
New
Okay, I'm having a heck of a time trying to figure out how to best handle the validation of belongs_to associations in Ecto. I'm sure I'...
New
Hi all,
Trying to get some more clarity over utc_datetime and naive_datetime for Ecto:
https://hexdocs.pm/ecto/Ecto.Schema.html#module-...
New
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
i’m a new one to elixir
which editor can i use
vs code? or atom?
Thanks! :smiley:
New
I’m working on defining a simple Ecto schema for a table (in PostGres), but I don’t see where I can define a column as NOT NULL. Conside...
New
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
Hi all,
I've just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage.
I'm trying to use Postg...
New
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: )
Hello all, this is ...
New







