-
CodeView full code...
const OneComponent = () => ({
// component structure
meta: {
title: "A Component",
data: ["x", "y", "z"],
endpoint: API + "data/",
show: true,
},
data: [0, 0, 0],
async init() {
[this.data[0], this.data[1], this.data[2]] = await Promise.all([
UTILS.fetchText(this.meta.endpoint + "x/"),
UTILS.fetchText(this.meta.endpoint + "y/"),
UTILS.fetchText(this.meta.endpoint + "z/"),
]);
// refresh data every 3 seconds
setTimeout(() => this.init(), 3000);
},
});
const OtherComponent = () => ({
// same structure as above
});
// more components with same structure...
<aside x-cloak x-data="OneComponent" x-show="meta.show" x-transition>
<h3 x-text="meta.title"></h3>
<hr />
<p>
<span x-text="meta.data[0]"></span>:
<span x-text="data[0]"></span>
</p>
<p>
<span x-text="meta.data[1]"></span>:
<span x-text="data[1]"></span>
</p>
</aside>
<aside x-cloak x-data="OtherComponent" x-show="meta.show" x-transition>
<!-- same innerHTML as above -->
</aside>
<!-- rest of the components... --> GoalI want to be able to do something like this...
const OneComponent = () => ({
// component structure
meta: {
title: "A Component",
data: ["x", "y", "z"],
endpoint: API + "data/",
show: true,
},
data: [0, 0, 0],
async init() {
[this.data[0], this.data[1], this.data[2]] = await Promise.all([
UTILS.fetchText(this.meta.endpoint + "x/"),
UTILS.fetchText(this.meta.endpoint + "y/"),
UTILS.fetchText(this.meta.endpoint + "z/"),
]);
// refresh data every 3 seconds
setTimeout(() => this.init(), 3000);
},
});
const OtherComponent = () => ({
// same structure as above
});
// more components with same structure...
const AllComponents = {
one: OneComponent,
other: OtherComponent,
// rest of the components...
}
<section x-data="{ components : AllComponents }">
<template x-for="component in Object.keys(components)">
<aside
x-cloak
x-data="component"
x-show="meta.show"
x-transition
>
<h3 x-text="meta.title"></h3>
<hr />
<template x-for="(item, i) in data">
<p>
<span x-text="meta.data[i]"></span>:
<span x-text="item"></span>
</p>
</template>
</aside>
</template>
</section> ProblemI'm having trouble with nested RequestAny help regarding how to make such reusable components would be much appreciated. PS: This is a client-side application. There will be no server-side processing available (assume GitHub Pages hosting environment). |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
The issue in this example is that I'm pretty sure you want the values like Nested loops do just work there isn't anything special you need to do with them |
Beta Was this translation helpful? Give feedback.
The issue in this example is that
Object.keys()
returns the keys of the Object. Not the values.I'm pretty sure you want the values like
Object.values()
Nested loops do just work there isn't anything special you need to do with them