Sebb
Send event from JS to liveview
I’m facing a problem like this one here: Multi-select with Alpine.js - Help - Livewire Forum - send the currently selected options from a custom multiselect (implemented in alpine, I’m using this one here: Multi select alpine js | Alpinejs, Buttons, Forms, Selects).
There is already a thread about this (I can’t find right now) and I think the easiest way to do it is with a hook:
Hooks.RelayHook = {
mounted() {
relay = this
document.addEventListener('relay-event', (e) =>
relay.pushEvent(e.detail.event, e.detail.message),
)
},
}
...
window.dispatchToLV = function (message) {
let relay_event = new CustomEvent('relay-event', {
detail: message,
})
document.dispatchEvent(relay_event)
}
<div id="relay-hook-el" phx-hook="RelayHook" hidden></div>
But now seeing, that livewire can do it out of the box, I’d like to ask if there is a better way and if not, I think there should be a better way.
Trending in Discussions
As the title says, please share what you’ve been up to with Elixir. Whether that’s been learning it, looking into it, making stuff with i...
New
@chrismccord : I just saw the Extract AGENTS.md from Phoenix.new into phx.new generator commit to the phoenix project.
My initial shotgu...
New
I was working on an Ecto migration and I needed a timestamp. So, for the nth time, I looked up the different data types for timestamps, a...
New
Just a general thread to post chat/news/info relating to AI/ML stuff that may be relevant for Nx now or in the future. Got anything to sh...
New
I just stumbled on a newly redesigned elixir-lang.org. :tada: It looks like @Software_Mansion did the work, and I think it is generally a...
New
There has been a thread to discuss the Stack Overflow Developer Survey on this forum every year since 2018, so here’s yet another one for...
New
Fly’s CEO posted this recently - Turn And Face The Strange · The Fly Blog
It says that Fly is going all-in on sprites, which is a worry ...
New
Other Trending Topics
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve.
They are GUI (Emerge) and State management (S...
New
Emily is an Elixir library that runs Nx computations on Apple’s MLX. Install it as the default Nx backend and Nx, defn, Axon, Nx.Serving,...
New
@hugobarauna and I (Alex Koutmos) have been hard at work on writing a book on Nerves that takes you from simply blinking LEDs to building...
New
There are three potential reasons for members of this forum to have a look at https://vutuv.de
You are tired or annoyed of LinkedIn.
Yo...
New
Latest Phoenix Threads
Chat & Discussions>Discussions
Latest on Elixir Forum
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #elixirconf-us
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #metaprogramming
- #hex
- #performance










First 4 of 4 Posts
soup
Not sure I quite understand the question.
If you are ok to alter the shown component, you can add
phx-click={JS.push("selected_option", value: %{id: id})}on the “drop down option div”, which would ping an event out for each separate option cilck. You would have to track theremoveevent separately and do all the list maintenance yourself. You could wrap this in an functional component.You could also adjust the given component to dispatch a custom event when a selection change occurs, and that event could contain all the options selected. Have a hook that catches that and pushes all the options in one go. There was a talk at this years ElixirConf about using custom html components which would fit this very well if you wanted to drop alpine. I assume this is what you’ve basically already done.
Or you could generate a small form inside the dropdown with alpines
x-for, with hidden inputs and generate asubmitevent on it when selection changes and use the normalphx-submit/changestuff. This will have issues embedding in other forms but you could just use the form it’s embedded in in that case.The hooks probably the easiest to maintain in the end I think. One event with the whole set of selected options. Phoenix LiveViews provided events aren’t designed to be “input specific” (i.e. you cant
phx-changeon an<input>), so it’s not really designed to work like LiveWire’s model binding, I would think.Sebb
Sorry, the question was a little sloppy.
I just stumbled upon that livewire post and thought, that they have a simple way to just send arbitrary events to the server - I’d like to have that for LV. With LV the only way to do this is with a hook (right?). The hook is OK, but the element I need in the HTML is a little annoying.
But I think I did not understand the livewire solution right, they just simulate an
inputevent. I can do the same with LV:Phoenix.HTML.multiple_selectfor this<select>by toggling<option selected ...>phx-changewithsee: JavaScript interoperability — Phoenix LiveView v1.2.5
Now the fancy-alpine-multiselect behaves just like a
multiple_select. Nice.So I do not need to send a custom even here, but still it would be nice if it was possible.
derek-zhou
FOr non-UI related general event, I use a hook on the base container of my liveview.
Sebb
like this?
why did I never think of that?