tfwright

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?

First 3 of 3 Posts! Switch mode

stryrckt

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().

11
Post #1
tfwright

tfwright

Thanks, your article looks like it has a lot of useful information.

Attaching the hook to the window is, as you say, a bit crude, but it certainly seems like a crucial tool when using Alpine, especially if the alternative is dropping in script tags where ever you have even slightly complex UX logic.

On the other hand, I wonder though, if there is that much logic required for a component, maybe something more fully featured like Vue would be a better fit than Alpine in such a case. But I don’t really see myself trying to use LiveView, Alpine, and Vue all in a single project. Just thinking out loud…

cj5

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>

Where Next?

Trending in Discussions Top

AstonJ
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...
2976 91332 914
New
f0rest8
Hi everyone :waving_hand: Posting here to showcase and announce that Metamorphic is now officially live on a public-facing domain at htt...
New
byu
@chrismccord : I just saw the Extract AGENTS.md from Phoenix.new into phx.new generator commit to the phoenix project. My initial shotgu...
New
arcanemachine
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
AstonJ
Just a general thread to post chat/news/info relating to AI/ML stuff that may be relevant for Nx now or in the future. Got anything to sh...
New
type1fool
I just stumbled on a newly redesigned elixir-lang.org. :tada: It looks like @Software_Mansion did the work, and I think it is generally a...
New
juhalehtonen
There has been a thread to discuss the Stack Overflow Developer Survey on this forum every year since 2018, so here’s yet another one for...
New

Other Trending Topics Top

JesseHerrick
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
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge &amp; Solve. They are GUI (Emerge) and State management (S...
New
ausimian
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
akoutmos
@hugobarauna and I (Alex Koutmos) have been hard at work on writing a book on Nerves that takes you from simply blinking LEDs to building...
New
zachdaniel
Introducing AshStorage! Attachment and file management that slots directly into your resources :smiling_face_with_sunglasses: I had hope...
New

We're in Beta

About us Mission Statement