hubertlepicki

hubertlepicki

Correct way to get LiveView Hook of element from JavaScript (alpine.js or otherwise)

I am wondering what’s the correct way to fetch an instance of Hook that’s related to given DOM node having it at hand in JavaScript (like, in Alpine.js script or a global script initiated outside of Hooks).

I figured out so far this way:

const el = document.querySelectoir(...);
const liveViewRootEl = window.liveSocket.root.el;
const hookInstance = window.liveSocket.getViewByEl(liveViewRootEl).getHook(el);

Is this the way? What I would like to do is to call functions on the LiveView Hook from Alpine.js code without the need to send messages through the element that is quite tiresome.

Most Liked

hubertlepicki

hubertlepicki

Yeah, that’s precisely why I decided to ask on the forum.

So I don’t have precise use case just yet, but I am evaluating alpine.js and how people are integrating it with Phoenix LiveView Hooks. The most comprehensive guide I found is here, and it’s full of ugly hacks. For example:

Hooks.Counter = {
  mounted() {
    window.counterHook = this
  },
  decrement() {
    this.pushEvent('decrement', {})
  },
  increment(selector) {
    this.pushEventTo(selector, 'increment', {})
  }
}

The above is how they turn the Hooks into something à la stimulous controllers, which I think is pretty neat. But the way they use global property on the window object is not great, and will quickly fall apart when you are about to render a list of Counters for example.

In the next section they send events from Alpine to Hooks, which is not great either as it goes through window instance and not the element in one way, and they are able to do it better when sending events from Hooks to Alpine.

I believe it would be both cleaner and less error-prone if we could, having a DOM element find instance of Hook that is, well, hooked to it. We can do the opposite quite easily.

The problem with the hacks I am seeing in that tutorial is that you need to do proper cleanup (which is not mentioned) in order to avoid double callbacks being installed, or memory leaks on the client in the browser if you leave unused callbacks set up on the window object, especially if those callbacks reference DOM nodes.

Having a two way integration would basically turn Hooks into Stimulous controllers on steroids.

Where Next?

Popular in Questions Top

jerry
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
_russellb
I want to try my hand at web scraping. What tools/libraries do I need to use. I’m hoping to turn this into something professional so don’...
New
fireproofsocks
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
makeitrein
Hey all, just started picking up Elixir last week and am writing a scraper as a learning project. Baby step #1 is extracting the number ...
New
minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
shahryarjb
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
LegitStack
I’m trying to make a websocket server in Phoenix or raw Elixir. I heard about gun, I think I could use cowboy, but since I’m not that sma...
New
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New

Other popular topics Top

JakeBecker
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
1140 51847 244
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
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 record...
New
jerry
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
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list....
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New

We're in Beta

About us Mission Statement