webuhu
As I found out that phx:update emitted JS event is nowhere documented,
I thought using the JS client hooks would probably the recommended way.
But if I start using the hooks for a lot of small things and different LiveViews,
the would get a very unorganized long list of hooks…
Isn’t there a good way how to organize hooks per views?
Source of my question: Displaying UTC DateTime in users browser TimeZone - #16 by webuhu
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
The obligatory hello world thread!
Who are you and where are you from? :stuck_out_tongue:
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
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
We’re evaluating API mocking tools for OpenAPI-based projects and would love to hear what other teams are using.
We’re particularly inte...
New
Is there a word for the ~> symbol used in Version strings?
Do you also just call it a Squiggle Arrow™ ?!
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
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
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
Latest Phoenix Threads
Chat & Discussions>Discussions
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
- #blog-post
- #phoenix_html
- #iex
- #ai
- #graphql
- #genstage
- #elixirconf-us
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #metaprogramming
- #security
- #hex










Showing Posts 1 to 2- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
aseigo
I’ve been using liveview extensively on a reasonably large project over the last 4 months or so, and as it developed I ended up pulling in a few different client-side JS libraries for things like D’n’D list sorting, autocomplete in textareas, maps, etc. which ended requiring hooks for interop with liveview.
In the process of development I’ve come to two patterns:
Pattern A: I am doing it wrong.
Sometimes I have found that I’ll be ‘fighting’ to get around liveview unnecessarily and where a hook seemed like a good solution, it was really easier to just let liveview do its thing. Hooks should really only be used when absolutely needed.
Pattern B: Registering hook functions based on triggers. I have a handful of components on the front end that need something to run when the page is first loaded, others when the specific component is mounted, others whenever they are updated, etc. I had started out making hooks for the various components, but realized that they were verrrrry repetitive and when making changes to the front end, I’d have to make subsequent changes to the hooks in app.js. So what I’ve done instead is create a few generic hooks, such as this one:
and then in the page-specific JS (I have modified the root template to check for js and css files in the @assigns to include specific cs/jss for different pages), something like this:
.. and in the leex template file:
This has let me have a small collection of common Hook patterns which dispatch to functions that are registered by the page JS.
Still, I use them sparingly, and only when I haven’t entered Pattern A
Also, related, perhaps .. I find myself using pushEvent/push_event on both the client and server side to get information flowing between front end libraries and the liveview backend: when a front-end lib triggers some event in JS, it can be forwarded on to the liveview over the websocket easily, and when the backend receives an update that needs to be reflected in a component managed by a front-end library it can send those to the front end using push_event which is easily hooked up to client-side js as they enter the front end as, well, events which listener functions can be added to.
webuhu
Thanks a lot.
Probably I also need to fight against Pattern A.
I’m often thinking I should avoid the network round trip.
But Pattern B sound like a reasonable solution.
I definitely need to try it in that fashion.