It’s long time I’m trying to work out this issue. I would like to create a number of tabs
based on the data fetched from a API in JSON format. The number of tabs is not fixed but it depends on the data downloaded. The code below creates correctly the tabs
according to the JSON data, it populates the page
but - of course - it displays the same data for each page because the content of datas
is overwritten by the forEach
.
<App>
<JavaScript>
var Observable = require("FuseJS/Observable");
var datas = Observable();
var occurrences = Observable();
var json = {
"category": [
[
"Music",
"Theatre",
"Shows"
]
],
"events": [
{
"title": "Music. Faucibus dignissim parturient.",
"occurrence": "Music"
},
{
"title": "Music. Taciti ante scelerisque consequat.",
"occurrence": "Music"
},
{
"title": "Theatre. Condimentum adipiscing leo.",
"occurrence": "Theatre"
},
{
"title": "Shows. Ut vestibulum tincidunt consectetur.",
"occurrence": "Shows"
},
{
"title": "Shows. Parturient mi urna commodo.",
"occurrence": "Shows"
},
{
"title": "Shows. Pharetra pretium nisl felis.",
"occurrence": "Shows"
}
]
};
module.exports = {
data: datas,
occurrence: occurrences,
occurrenceCount: occurrences.lenght,
};
// Get 'category' values and add them to 'occurrences'
json.category.forEach(function(item){
occurrences.replaceAll(item);
});
// for each category (occurrences) get 'events'
occurrences.forEach(function(entry){
//console.log(entry);
json.events.forEach(function(item){
// There's should be a different 'datas' for each category (occurrences)
// that should be binded below to it's proper tab
datas.add(item);
});
});
// Debug. Output events
datas.forEach(function(events) {
console.log(events.title);
});
</JavaScript>
<ClientPanel>
<Panel Dock="Top">
<PageIndicator Navigation="mainNav" Orientation="Horizontal" ItemSpacing="10" Alignment="Left" Margin="5">
<DockPanel ux:Template="Dot" Color="#CCC" Padding="10,5">
<Text Color="#000" Value="{Page Title}"/>
<Clicked>
<NavigateTo Target="{Page Visual}"/>
</Clicked>
</DockPanel>
</PageIndicator>
</Panel>
<Page ux:Class="MyPage" Color="#AAA">
<ScrollView Margin="0,0,0,0">
<StackPanel ItemSpacing="6" >
<Each Items="{data}">
<Text Value="{title}" />
</Each>
</StackPanel>
</ScrollView>
</Page>
<PageControl ux:Name="mainNav">
<Each Items="{occurrence}">
<MyPage Title="{}" />
</Each>
</PageControl>
</ClientPanel>
</App>
(The structure of the JSON can be modified if needed as I did the API by myself.)
-
I don’t know how I could set/create an Observable that could keep the fetched data subdivided by category
-
Once I have this Observable I don’t know how to dinamically bind it to the UX.