tfwright
Alpine.js and Phoenix LiveView hooks: both needed?
Hearing that it now works seemlessly with LiveView, I recently added alpine.js to a project in order to handle some dynamic UI state. It worked great, and I moved some related code out of a LiveView hook into the new alpine template in order to keep it simple/organized. While doing so I had the idea that pretty much everything in the hooks could be moved to alpine functions, with the exception of pushEvent calls.
Anybody else using alpine and LV together have thoughts about the proper role for hooks? I there a way to call pushEvent outside the Hooks context?
Most Liked
stryrckt
I wrote an article on using LiveView with Alpine.js.
Even with Alpine.js, hooks are useful for handling LiveView lifecycle events and are required to use the new push_event functionality introduced in 0.14. Also, if I have a big chunk of JavaScript code, I would rather put it in a hook function than in my HTML and then call it from Alpine.js code.
You can’t call pushEvent directly from Alpine.js code, but you can proxy through a hook. Since hooks are regular JavaScript objects, you can register the proxy hook on window and then call pushEvent through that.
Here is an example from the article:
Hooks.PushEvent = {
mounted() {
window.pushEventHook = this
}
}
<div id="counter"
phx-hook="PushEvent"
x-data="{}">
<h1>The count is: <span><%= @count %></span></h1>
<button @click="pushEventHook.pushEvent('decrement', {})">
Decrement
</button>
<button @click="pushEventHook.pushEventTo('#counter', 'increment', {})">
Increment
</button>
</div>
That said, I find creating a general PushEvent proxy hook to be somewhat crude. Typically I will create more purposeful functions on my hooks that call pushEvent as part of their implementation. So in the above example, I would create increment and decrement functions on a CounterHook, registered on window as counter, then call them with counter.increment() and counter.decrement().
cj5
I know it has been a while since this discussion, but I think the Phoenix.LiveView.JS API provides a good way to push events from JS.
<button
class="btn"
@click.prevent="liveSocket.execJS($el, $el.dataset.decrement)"
data-decrement={JS.push("decrement", target: @myself)}>-</button>
Popular in Discussions
Other popular topics
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
- #websockets
- #supervisor
- #advent-of-code
- #elixirconf-us
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #performance









