larshei

larshei

Push_event from components

What I want to achieve

I am currently building a small frontend displaying a JavaScript based map, with markers for positions of assets. The map itself is a Component.

There are basically just two pages:

  1. An overview page listing all assets
  2. A detail page per asset.

I can update the map using push_event/3, what works well on the details page:
There are two events, add_asset and update_asset.
On mounting the liveview, the asset is added to the map.
When new data comes (in my case in a handle_info), the asset position is updated.

I would like to add a map to the overview page, listing all assets positions.

What I tried to achieve this

As there already is a function to add an assets position to the map, I thought I’d just use that.
The overview page renders a bunch of Components, of which one is the map.
The other components are simply list entries, one per asset. All components are stateless.
I tried to push_event/3 an add_asset event from each components update() callback.

Expected behaviour
I would have expected each asset to be added to the map.

Actual behaviour
Nothing is added.
Each attempt to add an asset should trigger a console.log(), but nothing is logged.

Question
Seems like the events are never actually pushed out to browser.
Does pushing events from within a components update() function not work?
Or maybe, do these occur before the socket connection is established?

First Post!

tomkonidas

tomkonidas

When you push_event/3 your are pushing an event to the client to be consumed by hooks. So you would have to have a JS hook that would catch that event to send back to the backend.

Are these 2 pages different live views? (i.e. Listing.Index and Listing.Show)

Are you mounting the live_component in each view?

Can you paste some code so we can maybe see a little whats going on?

Your JS hooks, LiveView(s) and your component would be sufficient

Where Next?

Popular in Questions Top

RisingFromAshes
I’ve read in another post that it may be possible with a router helper - but I couldn’t find an appropriate one, and tbh, I’m still just ...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
hariharasudhan94
Lets say I have map like this fetching from my database %{"_id" => #BSON.ObjectId<58eb1a7a9ad169198c3dXXXX>, "email" => ...
New
lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New

Other popular topics Top

electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
Brian
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
JakeBecker
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
1144 55125 245
New
dogweather
I wrote this comment on r/haskell, and it’s not popular there. :wink: But I think I’m on to something… Haskell reminds me of Java, and e...
New
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New

We're in Beta

About us Mission Statement