CherryPoppins
Liveview blowing up Alpine state?
I’m having an issue with alpinejs where I am seemingly loosing alpine after performing a task where liveview basically validates a changeset and reassigns/updates @changeset, I don’t think this part is important to the issue. While this changeset is getting validated I have a loader that uses x-show to show it or hide it based on a bool value from an Alpine.store. The loader is turn on with an @click and turned off by the liveview sending a message to a hook that changes the bool in Alpine.store. The loader is getting stuck to on/show (or its natural state of display: block) because I am loosing that x-show. Typing Alpine.start() into the console fixes the issue so I know it is alpine getting uninitialized or not loading back up or something. Anyone else having this issue with liveview and alpine?
Alpine setup:
import Alpine from 'alpinejs'
window.Alpine = Alpine
Alpine.start()
...
const liveSocket = new LiveSocket('/live', Socket, {
params: {_csrf_token: csrfToken},
dom: {
onBeforeElUpdated(from, to) {
if (from._x_dataStack) {
window.Alpine.clone(from, to)
window.Alpine.initTree(to)
}
},
},
hooks: Hooks,
})
May or may not be relevant to this issue but I am also getting this error sometimes in the console on this liveview:
module.esm.js:2288 Uncaught (in promise) TypeError: i is not a function at module.esm.js:2288
the function, which seems to be coming from alpine, the error is referring to with the call to i() (23 lines down)
window.Element.prototype._x_toggleAndCascadeWithTransitions = function(el, value, show, hide) {
let clickAwayCompatibleShow = () => requestAnimationFrame(show);
if (value) {
el._x_transition ? el._x_transition.in(show) : clickAwayCompatibleShow();
return;
}
el._x_hidePromise = el._x_transition ? new Promise((resolve, reject) => {
el._x_transition.out(() => {
}, () => resolve(hide));
el._x_transitioning.beforeCancel(() => reject({isFromCancelledTransition: true}));
}) : Promise.resolve(hide);
queueMicrotask(() => {
let closest = closestHide(el);
if (closest) {
if (!closest._x_hideChildren)
closest._x_hideChildren = [];
closest._x_hideChildren.push(el);
} else {
queueMicrotask(() => {
let hideAfterChildren = (el2) => {
let carry = Promise.all([
el2._x_hidePromise,
...(el2._x_hideChildren || []).map(hideAfterChildren)
]).then(([i]) => i());
delete el2._x_hidePromise;
delete el2._x_hideChildren;
return carry;
};
hideAfterChildren(el).catch((e) => {
if (!e.isFromCancelledTransition)
throw e;
});
});
}
});
};
Most Liked
kuroda
I ran into a similar problem; adding an id attribute to an element with the x-data attribute did not solve my problem.
After some trial and error, I found the following workaround:
First, remove window.Alpine.initTree(to) from onBeforeElUpdated.
Then, add the phx-hook="AlpineRoot" attribute to the element with the x-data attribute or one of its ancestor elements.
Finally, define the following hook in your app.js:
Hooks.AlpineRoot = {
update() {
window.Alpine.initTree(this.el)
}
}
I hope my experience will be useful to someone.
arcanemachine
I’m a big fan of Alpine.js and it’s good to see others getting it to work with LV.
Have you seen this other thread on the topic? There’s some snippets in there that are also useful on working around some of the issues with Alpine:
Popular in Questions
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










