1

I'm trying to promote a content on a page. This content should the upcoming content, i.e. the next one in all of the same type ("event").

Here's how metadata look like :

---
layout: event
date: 2013-12-11
title: Cool event
---

The docpad.coffee file collections configuration looks like (using moment.js):

collections:
    events: (database) ->
        database.findAllLive(type: "event", [date: -1])

    next_events: (database) ->
        @getCollection("events")
            .setFilter("upcoming", (event, searchString) ->
                return moment(event.eventDate, "YYYY-MM-DD").diff(moment()) < 0
            )

Finally in the index.html.eco :

<% for event in @getCollection("next_events").toJSON(): %>
    <h3><%= event.title %></h3>
<% end %>

Problem: it shows all my events, not only the future ones. When I use the events collection it works as intended (all events ordered by date desc). But no difference when I use the next_events collection. I know that to show only one I'll need to reduce the collection content with something like <% for event in @getCollection("next_events").toJSON()[0]: %>.

Any pointer greatly appreciated.

DjebbZ
  • 1,594
  • 1
  • 18
  • 34

1 Answers1

2

Try this:

next_events: (database) ->
    @getCollection("events")
        .createLiveChildCollection()
        .setFilter("upcoming", (event, searchString) ->
            return moment(event.eventDate, "YYYY-MM-DD").diff(moment()) < 0
        )
        .query()
balupton
  • 47,113
  • 32
  • 131
  • 182
  • Doesn't work, it still displays them all. If I change the comparator (from < to >) it displays nothing. I'm pretty sure the problem comes from the filtering part. I tried different functions from moment.js, with no luck. – DjebbZ Sep 09 '13 at 07:36
  • 1
    `event` is actually going to be a model there, so if `eventDate` is an attribute, you probably need to do `event.get('eventDate')` – balupton Sep 09 '13 at 16:38
  • 1
    Perhaps DocPad needs to provide an interactive CLI to access the database so you can expirement with queries and results – balupton Sep 09 '13 at 16:39
  • Stupid me, I know Backbone but forgot this detail. It almost worked, I tweaked 2 things : the date meta data is core Docpad and is a date object, not a string so I didn't to parse it with moment, I just wrapped it in a moment like "moment(event.get("date"))" ; in the template I retrieved the event like this : "<% next_event = @getCollection("next_events").toJSON()[0] %>". Then I just used the newly created next_event var. thanks a lot, you're the man ! +1 for the cli access to DB. – DjebbZ Sep 09 '13 at 17:38