Using data from Collections in Global Data (with a side of async) #2977
Replies: 2 comments 1 reply
-
Turns out I missed a section of the collections documentation:
That solved the initial problem I was running up against! That said, I still need a solution to the original question as my data structure is running into issues seemingly related to #461. For reference, here's some sample JSON that I'm trying to use with pagination. {
comicTitle: {
pages: [
[Object], [Object],
[Object], [Object],
[Object], [Object],
[Object], [Object],
[Object], [Object],
[Object], [Object],
[Object], [Object]
],
comicThumb: './comics/thumb.png',
length: 14
}
} My intent is to use a specified
A possible solution has been discussed in #332, but given the format and use case of my data I feel it would involve solving my initial problem first. |
Beta Was this translation helpful? Give feedback.
-
I think you're hitting https://www.11ty.dev/docs/pagination/#paging-an-object where you're paging an object and it's resolving // eleventy.config.js (snippet)
eleventyConfig.addCollection("comics3", async () => {
return {
pages: [
"One", "Two", "Three", "Four", "Five",
"Six", "Seven", "Eight", "Nine", "Ten",
],
comicThumb: './comics/thumb.png',
length: 14,
}
}); // index.11tydata.js
module.exports = () => {
return {
pagination: {
data: "collections.comics3",
size: 3,
resolve: "keys",
before(data) {
// console.log({ "this": this });
console.log({ data }); // { data: [ 'pages', 'comicThumb', 'length' ] }
return data.comicTitle.pages;
}
}
}
}; OUTPUTBecause 11ty uses { data: [ 'pages', 'comicThumb', 'length' ] } If we change to {
data: [
[
'One', 'Two',
'Three', 'Four',
'Five', 'Six',
'Seven', 'Eight',
'Nine', 'Ten'
],
'./comics/thumb.png',
14
]
} |
Beta Was this translation helpful? Give feedback.
-
I feel this might be a more Javascript specific issue, but I'm bringing it up here because it runs up against Eleventy's order of operations. What I'm trying to do is have
.addGlobalData
wait for a variable to be defined before it returns with data initially generated from.addCollections
. I've tried playing with promises/async, but data is consistently only available to templates after a refresh (assuming--serve
has been set).Stepping back, here's a run down of what I'm trying to do and why:
dist
folder. That's fine to me, because I'm more interested in utilizing those files as data (which I can use to copy them over manually)..addCollections
function that groups all the marked images together. That collection goes through a filter to clean the data for templates, but the ideal would be to work with global data instead. That way I don't have to utilize methods like pagination'sbefore
callback.What follows is a modified snippet of the code that I'm working with, focused on what I believe are the functions that are most relevant to this problem. If you need any further information, please do let me know! Thanks for taking the time to read this incredibly cursed question!
Beta Was this translation helpful? Give feedback.
All reactions