Skip to content

Instantly share code, notes, and snippets.

@SachaG
Last active June 8, 2017 15:15
Show Gist options
  • Save SachaG/a40373a83e54cd6da6e4 to your computer and use it in GitHub Desktop.
Save SachaG/a40373a83e54cd6da6e4 to your computer and use it in GitHub Desktop.
Meteor Pagination Package

Idea 1: Client Only

The user sets up the subscription and publication to take a postsLimit Session variable as parameter.

The pagination helper takes care of changing the Session variable on click.

Idea 2: Client/Server (see below)

Template

The template helper takes one argument: the name of the pagination.

Inside the template helper, you can use an if/else block to define the "load more" and "no more items" elements. The hasMore variable is automatically set for you.

Server

We create a new pagination object on the server.

Publication

The package should set up a posts publication that takes a limit parameter, and returns a cursor identical to the cursor taken as argument by the eachWithPagination helper, except extended with that limit parameter.

Subscription

The package should also automatically subscribe to the posts publication, using a postsLimit session variable.

Events

The contents of the if block are automatically assigned a click event handler that increments the postsLimit session variable.

URL

Optionally, the package could also reflect pagination changes in the URL: http://myapp.com/?posts-limit=15.

<template name="postsList">
{{#eachWithPagination posts}}
<h1>{{title}}</h1>
<p>{{body}}</p>
{{#if hasMore}}
<a href="#">Load More</a>
{{else}}
<p>No more posts<p>
{{/if}}
{{/each}}
</template>
new Meteor.Pagination({
name: 'posts',
cursor: Posts.find({}, {sort: {createdAt: -1}}),
start: 5,
increment: 10
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment