Class

Collection

Collection

This corresponds to a collection in Quintype. Most groups of content are modelled with this class.

See Collection.getCollectionBySlug for a simple example.

import { Collection } from "@quintype/framework/server/api-client";

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 (Level 1) - Sports Row (Level 2) (template- ThreeColGrid) - Cricket (Level 3) - Football (Level 3) - Tennis (Level 3) In the above example with nestedCollectionLimit: {ThreeColGrid: [2, 3, 4]}, Cricket collection will fetch 2 items, Football will fetch 5 items and Tennis will fetch 4 items. (default: defaultNestedLimit || 40)

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 (Level 1) - Sports Row (Level 2) - Cricket (Level 3) - Football (Level 3) - Entertainment Row (Level 2) - Movie (Level 3) - Song (Level 3) In the above example if we need to fetch the stories from Sports Row child collection we need to pass collectionOfCollectionsIndexes : [0], where 0 is the position of collection Sports Row and stories from Cricket and Football will be fetched

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:

View Source index.js, line 348

Promise.<(Collection|null)>

asJson()

Use this to convert to a simple javascript object, suitable for JSON.

View Source index.js, line 281