fteschke
I use this pattern to handle user input before JS has loaded / hooks have mounted:
<button onclick="this.$clicked = true" phx-hook="MyHook" phx-click={JS.dispatch('clicked')} />
// my_hook.js
mounted() {
this.el.addEventListener('clicked', () => { ... })
if (this.el.$clicked) {
this.js().exec(this.el.getAttribute('phx-click'))
}
}
This works quite well. But I would prefer a global solution rather than extending each relevant hook.
Ideally:
// app.js
document.querySelectorAll('[onclick="this.$clicked = true]').forEach(el => {
if (el.$clicked) {
window.liveSocket.execJS(el, el.getAttribute('phx-click'))
}
})
However, there is a timing problem.
If phx-click dispatches an event that the hook needs to handle, the hook must have been mounted (attached it’s event listener) first.
So I want to delay my app.js code until all hooks have been mounted.
Is there any way to do this (I was hoping for a phx:all-hooks-mounted event)?
Or is there an entirely different approach to achieving this?
Trending in Questions
Hello!
Suppose you are building workflow (order / task / payment) processing system with the following requirements:
Each workflow con...
New
Hi all,
I’m working on a Phoenix project where I need to make external HTTP requests and I’m trying to choose between Req and HTTPoison....
New
How do I tell Hologram not to escape HTML when substituting into templates?
{Textile.render(@product.long_description)}
escapes the HTM...
New
I’d like to create a file with 600 permissions (rw-------), but there’s a race condition if I create it and then change the permissions. ...
New
Hi,
we run different LLM models across a cluster, with each node loading a configurable subset. Model-specific queues let nodes consume ...
New
When I use Mix.install with a git dependency, using git@github.com:... how do I change the SSH identity used?
I’ve got two Github accoun...
New
Hello all. I have a question as to best practices for code organization when implementing a liveview with a lot of responsibilities.
Sup...
New
Other Trending Topics
Hey there,
I haven’t seen a list of useful Elixir specific skills for the popular AI tools yet so I’m starting one here with my first.
...
New
Hi everyone :waving_hand:
I wanted to share something I’ve been building — NowBlind, a random chat app designed to help people discover ...
New
I fully migrated to my own harness from Anthropic/Gemini and I think it’s time to share it. Welcome DSH, the DeepSeek Harness, fully writ...
New
Hey. Is there anyone here who creates agents in their apps? Not talking about using agents, but creating them. I’m finding it pretty diff...
New
(I just needed to vent somewhere and LinkedIn is full of hope, or hype, I’m not sure which exactly)
AI dream has many faces, but general...
New
Localize is the next generation localisation library for Elixir. Think of it as ex_cldr version 3.0. The first version will be released ...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #code-sync
- #discussion
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #ai
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #blog-post
- #elixirconf-us
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #elixirconf-eu
- #processes
- #security
- #api
- #forms
- #performance










Showing Posts 1 to 9- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
LostKobrakai
You could probably do
phx-mounted={JS.dispatch("maybe-clicked")}fteschke
Afaict
phx-mountedis executed for element A before hook for element B has mounted.So I can’t use this as a global indicator of “all hooks have mounted”.
Instead, I would have to determine if an element is within a hook, and whether that hook has already been mounted.
I reckon that’s possible, just way more fiddly than I would like. And again, probably have to instrument each hook, or at least HTML where hook is referenced, individually.
fteschke
Maybe a mutation observer on all
[phx-hook]elements, and wait untilel.phxPrivate.mounted == true? Although I doubt the observer triggers for non-HTML attributes (phxPrivate).LostKobrakai
Why do you need that? You’re already sending individual clicks to the server.
fteschke
Sorry, I’m not talking about sending the clicks to the server. I should have been clearer about this.
This is about client-side handling only.
E.g. user clicks dropdown button or sidebar toggle button before JS bundle has loaded.
LostKobrakai
I still do not see why that would require some global tracking.
dimitarvp
You could just disable the whole thing until LiveView is fully ready on the page. From then on it’s business as usual.
Not the most elegant solution but I’ve very rarely stumbled upon users who would nervously click on the page 0.5s after they typed an URL.
fteschke
Agreed, disabling the interactive elements would work. Best without any visual style changes (suppress any button ‘disabled’ styles) to avoid flickering on page load.
Similar line of thought: Optimize page load times (small JS bundle, chunk splitting, …, check caching).
Even so, if any one has thoughts in case above is not enough, please let me know.
fteschke
I want to handle click events that occurred before the JS bundle loaded.
Such events may be handled by JS hooks.
For the event to be properly handled, the respective hook may have to be mounted and have registered its event listeners.
So before I can “retry” the
phx-clickhandler for each element that was early clicked, I need to make sure that any hook that may be listening to events dispatched from thatphx-clickhas been mounted.I can solve this problem per element/hook by knowing which elements and hooks are related.
But I would rather have a global solution that doesn’t require this level of knowledge. In that case I think the easiest solution is to wait until all hooks have mounted before “retrying” the
phx-clickhandlers.