See http://stackoverflow.com/a/14381327/130638 for explanation.
-
-
Save llazzaro/980c07c32b348aee2c2b08a128dad417 to your computer and use it in GitHub Desktop.
DocPad: Query custom category objects (via parse event)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Prepare | |
_ = require('underscore') | |
categories = {} | |
# Configure DocPad | |
docpadConfig = | |
templateData: -> | |
getDocumentsForCategory: (categoryId) -> | |
return @getCollection('documents').findAll(categories: $has: categoryId) | |
getCategoriesForDocument: (document) -> | |
document ?= @getDocument() | |
categoryIds = document.get('categories') | |
categories = @categories[categoryId] for categoryId in categoryIds | |
return categories | |
getCategories: -> | |
return categories | |
events: | |
parseAfter: -> | |
@getCollection('documents').forEach (document) -> | |
documentCategories = document.getMeta().get('categories') | |
for documentCategory in documentCategories | |
# Update the global categories listing with our document's categories | |
categories[documentCategory.id] ?= {} | |
_.extend(categories[documentCategory.id], documentCategory) | |
# Update our document's categories with a simple id listing | |
document.set(categories: _.pluck(documentCategories, 'id')) | |
# Export | |
module.exports = docpadConfig |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment