revati

revati

Proposal: send_update accept cid

Hello, currently, to call send_update, i have to pass destination module and id (which is more of an html id than elixir side id - at least how i interpret it).

If i have live_component nested in another live component i do this currently

# Parent
<.live_component
  module={ChildComponent}
  id={"#{id}-child"}
  parent={{__MODULE__, id}}
/>
# Child
def handle_event("...", _, socket) do
  {parent_module, parent_id} = socket.assigns.parent
  send_update(parent_module, id: parent_id, action: my_payload)
end

I looked under the hood and all parent_module, id: parent_id does is gets cid to call further call correct callback (found by cid).

Wouldn’t it be easier just to

# Parent
<.live_component
  module={ChildComponent}
  parent={@myself}
/>
# Child
def handle_event("...", _, socket) do
  send_update(socket.assigns.parent, action: my_payload)
end
  1. if no id is given cid is used as id
  2. both live views and live components have cid
  3. cid can be used to call send_update to any target (both live views and live components).
  4. live views might aswell have update callback to handle those cases.

This also would move towards implementing this this:Provide a unified mechanism to message the parent/child · Issue #1597 · phoenixframework/phoenix_live_view · GitHub

Most Liked

atomkirk

atomkirk

Yes please! A case where i needed this was an autosuggest component. Arrow keys move slection up and down list of options, enter should select it and tell the parent. Theres no way to send a message to the parent programmatically!

Last Post!

trisolaran

trisolaran

You might find inspiration on how to do that here: GitHub - maxmarcon/live_select: Dynamic (multi)selection field for LiveView · GitHub
TL;DR: to send an event to the parent you can use a hook. Yes, not ideal, because you go through the frontend, and yes, a way to be able to message the parent with a unified API is IMO very much needed.

Where Next?

Popular in Proposals: Ideas Top

sevensidedmarble
All the pieces are there to execute arbitrary JS commands from the server. You can put them on a data property and call them from the cli...
New
dimitarvp
To @jonatanklosko and @the-mikedavis: I see that there is a Rust crate at crates.io: Rust Package Registry but it is pointing at https:/...
New
mxgrn
As one edits a Phoenix LiveView form in a modal, it’s very easy to accidentally press ‘Esc’ and lose all the edits. Similar to data-conf...
New
lessless
Hi, There are a few writeups describing alternative config arrangements by a topic (or an OTP app): Configuring Phoenix apps: Two sma...
New
shahryarjb
When proposing or suggesting something please consider: As my experience implementing getBoundingClientRect as Phoenix.LiveView.JS funct...
New
New
nunobernardes99
On 1.8+, when we generate an authentication system with mix phx.gen.auth we can make use of magic link login which is amazing and a great...
New

Other popular topics Top

joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 records...
New
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
axelson
This post is a wiki (feel free to hit the edit button near the bottom right of this post to add your own changes!) This post collects co...
239 49134 226
New
gausby
I asked this very same question on twitter and got some interesting feedback, but I thought it would be a good question to ask here as we...
1207 40082 209
New
AstonJ
Seen any cool LiveView demos, sample apps or examples? Please post them here! :003:
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New