tfwright

tfwright

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?

Showing Posts 1 to 3

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 OP

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>
— All posts loaded —

Where Next? Top

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...
2977 92995 915
New
caslu
I want to open this thread for you all to discuss and help those who really like Ash but are still hesitant to use it in a real project. ...
New
mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
GES233
I’m posting this in response to Jose’s recent tweet (Cr. link) : People are sleeping on Elixir for a coding harness: Hot-code swappi...
New
marciol
It would be helpful to have a list of companies worldwide that hire engineers without prior experience in Elixir. Often, it can be quite ...
New
durvia
Anyone running long-lived stateful processes on BEAM? We’re building an AI agent runtime and would love to compare notes. We’re a small ...
New
nseaSeb
I’ve just put together a small POC exploring PDF inspection from Elixir/Phoenix: The idea is pretty simple: drag &amp; drop a PDF in a...
New

Other Trending Topics Top

garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
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
Dmk
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
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
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews