Search code examples
mongodbmeteorpaginationmeteor-blaze

How can I page through one document at a time using Blaze in Meteor without multiple mongo queries


I have a mongo database of task documents where tasks look something like this:

{
  "name" : "Clean room",
  "points" : 5
}

I would like a simple interface where users can have a carousel of sorts, where they can cycle through a rolodex of their tasks but only see one task at a time.

I've thought about doing some sort of mongo query such as

Tasks.find({}).limit(1).skip(index);

but that seems very inefficient, both for number of DB requests and in time the user needs for the request to be made.

Is there a way that perhaps a query could return a cursor object, and then show only the current indexed task? I would hesitate to change the cursor into a local array because I would lose reactivity. Bonus points if I don't have to install a pagination or reactive-array/dict library, but I welcome the suggestions.


Solution

  • How about just deciding which element to show with an additional helper?

    <template name="myTemplate">
      {{#each tasks}}
        {{#if taskVisible @index}}
          {{> oneTask}}
        {{/if}}
      {{/each}}
    </template>
    

    Where tasks is a helper that returns a cursor and oneTask is the template you want to use to display an individual task.

    Then it's just an issue of writing the taskVisible helper which decides if a given task is visible or not.