Return Alpine proxy data object #4375
asbjornu
started this conversation in
3. Feature Ideas
Replies: 1 comment 10 replies
-
This wouldn't work, purely because You pass a component builder. The component is then initialized every time that data function is used in your document. If you want one global one like this, then https://alpinejs.dev/globals/alpine-store Is there a reason why Alpine store doesn't work for you? |
Beta Was this translation helpful? Give feedback.
10 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
As with #203, I would like to interact with an Alpine data component through out-of-scope JavaScript. I know I can use events to do communicate between the outside and inside, but as this particular case is a JavaScript class holding some state, which given some external conditions, eventually initializes an Alpine component, that's a bit cumbersome. All the communication would be between the outer class and the inner Alpine Data component, after all.
I could of course merge all of the surrounding code into the Alpine component, but that makes the interaction with the JavaScript class from both itself and from the outside very awkward. Not being able to use private fields,
static
methods, etc. is very limiting.While following the Alpine.js best practice usually works fine, being able to interact with an Alpine data component more ergonomically from JavaScript is highly desirable.
There are currently at least two hacks available to get around this. One is
document.querySelector('["x-data"]').__x
and another is to wrap the initialization of Alpine in aPromise
:An imho better option would be for the
Alpine.data()
method to return the proxied data object. As it currently returnsundefined
, that would be a non-breaking change that would remove the need to use the hacks described above.Beta Was this translation helpful? Give feedback.
All reactions