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
- Why is the
x-initrunning 4 times and how can I make it only run once? - How can I get my hook to execute before
x-initor thealpine:initevent runs?
First Post!
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
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!
Popular in Questions
Other popular topics
Latest Phoenix Threads
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
- #elixirconf-us
- #advent-of-code
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #hex
- #security









