Replies: 2 comments 4 replies
-
Can you make a codepen? I use x-for with store a lot and have not seen it fail so I suspect something strange is happening and codepen would help to investigate. |
Beta Was this translation helpful? Give feedback.
2 replies
-
This is an expected behavior. Alpine only works on elements which has There are few issues in your jsfiddle:
Your code should be something like this. <div id="1" x-data>
<ul>
<template x-for="notification in $store.notifications.list">
<li>
Message: <span x-text="notification.message"></span>
</li>
</template>
</ul>
</div>
<script>
document.addEventListener('alpine:init', () => {
Alpine.store('notifications', {
list: [{
message: "happy chocolate berry"
}, {
message: "fishcakes supreme"
}, {
message: "cheeseburger"
}]
})
})
</script>
|
Beta Was this translation helpful? Give feedback.
2 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Not sure if this is a bug or just documentation that needs to be clarified, but
x-for
referencing$store
fails to evaluate unless the parent element has anx-data
set (even if just defining an empty object).When you specify
x-data
on the parent element,x-for
will fail if you are referencing a$store
that is not set yet (because the store is created after thealpine:init
event.The workaround ends up having to watch the state object and hoist it's state out into the parent elements
x-data
(however you need to wrap it in asetTimeout
for the$store
to be defined) 🤦🏿♂️ .some code samples below, id's and classnames stripped for brevity.
Doesn't evaluate the x-for at all
Evaluates the x-for, but throws an error as the store isn't created yet
Works as expected, but seems hacky
Suggestions would be to clarify in the documentation this behaviour, or to make x-for work correctly with $store (i.e: waiting for it to exist)
Beta Was this translation helpful? Give feedback.
All reactions