How to combine components by namespace in x-data? #702
Replies: 2 comments 2 replies
-
@jameslkingsley If the component properties don't cross over, you could spread each one out:
Now, you can access |
Beta Was this translation helpful? Give feedback.
-
Yea I considered the spread with prefixing the variable names, it's just a bit cumbersome after a while to do that. Just had an idea... It would be cool if Alpine could separate these scopes using something like: <div x-data.sidebar="{ open: false }" x-data.foo="{ open: false }">
<button @click="sidebar.open = true"></button>
<button @click="foo.open = true"></button>
</div> Then internally that would manage two separate reactive objects, and each object would only have access to itself. Could this be something supported by Alpine? I might look into a PR if it doesn't sound like a lot of work. |
Beta Was this translation helpful? Give feedback.
-
I want to build a set of reusable components that I can combine together into a single x-data. However, in order to keep it maintainable I want to keep them namespaced.
Eg:
In this example I should be able to do things like
<button @click="sidebar.open">
and utilise the methods and properties within each namespace.In the
app.sidebar()
method, it would then return an object like so:However, in practice this doesn't work because
this
inside each component does not refer to what you might expect. And even if I didthis.sidebar.opened
it still doesn't work.I'm not sure whether this is currently possible with Alpine, or maybe there's a different way to setup the exported function so that
this
refers to the object properly.Any help would be great!
Beta Was this translation helpful? Give feedback.
All reactions