Search code examples
javascriptconfigurationdocpad

How are DocPad collections configured using JavaScript? "warning: The custom collection XYZ is not a valid collection instance"


How can I configure DocPad collections when using JavaScript?

The following message is displayed during site startup: "warning: The custom collection myCollection is not a valid collection instance"

The configuration:

var docpadConfig = {
  collections: {
    // The collection causing problems.
    myCollection: function () {
      return [];
    }
  },

  templateData: {
    site: {
      url: "http://site-url.com",
      oldUrls: [],
      title: "Site title",
      description: "Site description",
      keywords: "DocPad",
      styles: ["/vendor/normalize.css", "/vendor/h5bp.css", "/styles/style.css"],
      scripts: ["/vendor/log.js", "/vendor/modernizr.js", "/scripts/script.js"]
    },

    getPreparedTitle: function () {
      if (this.document.title) {
        return this.document.title + " | " + this.site.title;
      } else {
        return this.site.title;
      }
    },

    getPreparedDescription: function () {
      return this.document.description || this.site.description;
    },

    getPreparedKeywords: function () {
      return this.site.keywords.concat(this.document.keywords || []).join(", ");
    }
  }
};

module.exports = docpadConfig;

From watching some YouTube clips of Benjamin Lupton I gather that DocPad uses Backbone.js - should I somehow import Backbone and use Backbone.Collection instances in the configuration? If yes, how do I do that? I found plenty of Coffee script examples but I can't seem to figure out how to apply them to plain JavaScript.

Any lengthy JavaScript configuration examples would be much appreciated, as I'm sure I'll want to tweak the config some more sooner or later :)


Solution

  • Got it working using the following:

    var docpadConfig = {
      collections: {
        journalEntries: function () {
          var documents = this.getCollection("documents");
          var sortByDescendingDate = [{ date: -1 }];
          // I've added "type" to the meta section of some documents.
          var typeEqualsJournal = { type: { $eq: "journal" } };
          var journals = documents.findAllLive(typeEqualsJournal, sortByDescendingDate);
          journals.on("add", function (model) {
            model.setMetaDefaults({ layout: "journal" });
          });
          return journals;
        }
      },
    
    ...