CherryPoppins

CherryPoppins

Alpine init issue

Let’s say I have the following in my liveview template:

my_template.html.leex
<div phx-hook="myHook">
  <div
    x-data="{inputvalue=''}"
    x-init="
      console.log('I AM INITING')
    "
  >
  <input x-model="inputValue">
  </div>
</div>
<script>
  document.addEventListener('alpine:init', () => console.log('INIT FROM SCRIPT TAG!'))
</script>

myHook.js
hooks.myHook = {
  mounted () {
    console.log('HOOK MOUNTED')
    document.addEventListener('alpine:init', () => console.log('INIT FROM HOOK'))
  }
}

My output is:

INIT FROM SCRIPT TAG
I AM INITING
I AM INITING
HOOK MOUNTED
I AM INITING
I AM INITING
  1. Why is the x-init running 4 times and how can I make it only run once?
  2. How can I get my hook to execute before x-init or the alpine:init event runs?

First Post!

ruslandoga

ruslandoga

I think x-init runs first when you get plain html response, then it runs again when liveview inits. Does it run 4 times without hook as well? If not, then I guess it might be forced to run after every hook event (mounted and updated in your case).

Also, how do you have alpine+liveivew set up?

Last Post!

CherryPoppins

CherryPoppins

Sorry the x-data="{inputvalue=''}" was a typo, in my actual code I have it as you said, x-data="{inputvalue: ''}".

On the Alpine.start(), I thought about that but it seems kinda hacky, and I’m also not always sending a message back to the hook on liveview calls so wouldn’t necessarily be able to always call Alpine.start() after liveview calls without going out of my way to do so. It seems I’m not the only on experiencing this and Alpine apparently doesn’t think it is a bug since they moved this ticket to discussion. Would have been nice to have gotten an explanation when moved. I appreciate you trying to help me out @ruslandoga!

Where Next?

Popular in Questions Top

jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set? Thanks.
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
Emily
I have VueJS GUIs with the project generated using Webpack. I have Elixir modules that will need to be used by the VueJS GUIs. I forese...
New
komlanvi
Hi everyone, I was playing with phoenix liveView but I run into an issue. I have a form and want to validate each input text when the te...
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New

Other popular topics Top

Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
jononomo
For some reason my phoenix channels are working for me in my local dev environment, but as soon as I deploy via Docker, I get a 403 error...
New
chrismccord
Phoenix 1.4.0 released Phoenix 1.4 is out! This release ships with exciting new features, most notably with HTTP2 support, improved deve...
688 31494 112
New
axelson
This post is a wiki (feel free to hit the edit button near the bottom right of this post to add your own changes!) This post collects co...
239 49084 226
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
dblack
I’ve got an issue with an app and I’ve no idea of how to troubleshoot it. I’m hoping someone here might have seen something similar. I p...
New

We're in Beta

About us Mission Statement