Methods
static getCollectionBySlug(client, slug, params, options) → {Promise.<(Collection|null)>}
This method returns a collection, given a slug. This is typically used for home and section pages.
If the result collection contains other collections, then it will recursively fetch those collections as well, upto a maximum depth of depth
.
Items that are collections will have item.story
set to a story map, and items that are collections will have the fields of that collection directly set on the item.
Instead of handling all edge cases yourself, this object can be used with the Collection Component
Example
const collection = await Collection.getCollectionBySlug(client, slug, {}, {depth: 3, defaultNestedLimit: 4, nestedCollectionLimit: {ThreeColGrid: [2, 3, 4, 2, 5], FullScreenSlider: [1, 2, 3, 4, 5]}});
if(!collection) {
render404();
} else {
recursivelyDebugCollection(collection);
// <Collection ... collection={collection.asJsoo()} />
showOnTheUI(JSON.stringify(collection.asJson()))
}
function recursivelyDebugCollection(collection) {
const items = collection.items || [];
items.forEach(item => {
if(item.type === 'story') {
console.log(item.story.headline)
} else if(item.type === 'collection') {
console.log(item["associated-metadata"]["layout"]);
recursivelyDebugCollection(item);
}
})
}
Parameters:
Name | Type | Description |
---|---|---|
client |
Client | |
slug |
string | The slug of the collection |
params |
Object | Parameters which are directly passed to the API |
story-fields |
string | The fields for stories. See DEFAULT_STORY_FIELDS for the default |
item-type |
string | Restrict the items returned to either "collection" or "story" |
options |
Object | |
depth |
number | The recursion depth to fetch collections. (default: 1) |
storyLimits |
Object | The limit of stories to fetch by collection template. This defaults to unlimited for templates that are not specified. (ex: {"FourColGrid": 12}) (default: {}). |
defaultNestedLimit |
number | The default limit of stories to fetch by each collection. (default: 40) |
nestedCollectionLimit |
Object | The number of stories or collection to fetch from each nested collection. (Ex: nestedCollectionLimit: {ThreeColGrid: [2, 3, 4]}).
eg:
- Home |
collectionOfCollectionsIndexes |
Object | It accepts array of indexes(collection's position) to fetch collection of collection of items when the depth is 1. (Ex: collectionOfCollectionsIndexes: [0, 4]).
eg:
- Home |
customLayouts |
Object | It accepts an array of objects to fetch the custom storyLimit and custom nestedCollectionLimit of custom layouts. (Ex: customLayouts: [{layout: "ArrowThreeColGrid", storyLimit: 9}, {layout: "ArrowTwoColTenStories", storyLimit: 2, nestedCollectionLimit: [5,5]}]). |
- See:
-
- GET /api/v1/collections/:slug API documentation for a list of parameters and fields
asJson()
Use this to convert to a simple javascript object, suitable for JSON.