I'm trying to update Liveview with Javascript after a Javascript event is fired.
Liveview must show a <div>
element with some values sent from Javascript.
My question is: how should I pass those values from Javascript to Liveview?
I might also need value sent by Liveview in Javascript. Again: how should I pass those values from Liveview to Javascript?
There is a Livesocket created in Javascript for liveview to work but I see no way to get or set assign values from there. The only way to pass values from/to Liveview seem to be via the DOM at some point. For example:
<div id="lv-data" phx-hook="JavascriptHook"></div>
let hooks = {};
hooks.JavascriptHook = {
mounted(){
this.el.addEventListener("jsEvent", (e) =>
this.pushEvent("jsEventToPhx", e.data, (reply, ref) =>
console.log("not sure what to do here")));
this.handleEvent("phxEventToJS", (payload) => console.log("data received: " + payload));
}
}
This feel weird to have to use the DOM with a dummy <div>
at all for pure data exchange...