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});
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 | |
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) |
- 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.