Skip to content

Commit

Permalink
Release 0.0.1
Browse files Browse the repository at this point in the history
  • Loading branch information
niftylettuce committed Jun 12, 2014
0 parents commit ec00677
Show file tree
Hide file tree
Showing 6 changed files with 441 additions and 0 deletions.
103 changes: 103 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@

# ------------------------------------------------------------------------------
# Common Node files and folders
# https://github.com/github/gitignore/blob/master/Node.gitignore
# ------------------------------------------------------------------------------

lib-cov
*.seed
*.log
*.csv
*.dat
*.out
*.pid
*.gz

pids
logs
results

node_modules
npm-debug.log


# ------------------------------------------------------------------------------
# Nodemon
# ------------------------------------------------------------------------------

.monitor


# ------------------------------------------------------------------------------
# JSHint
# ------------------------------------------------------------------------------

jshint


# ------------------------------------------------------------------------------
# Numerous always-ignore extensions
# ------------------------------------------------------------------------------

*.diff
*.err
*.orig
*.rej
*.swo
*.swp
*.vi
*~
*.sass-cache


# ------------------------------------------------------------------------------
# Files from other repository control systems
# ------------------------------------------------------------------------------

.hg
.svn
.CVS


# ------------------------------------------------------------------------------
# Your ideas
# ------------------------------------------------------------------------------

.idea


# ------------------------------------------------------------------------------
# OS X and other IDE's folder attributes
# ------------------------------------------------------------------------------

.DS_Store
Thumbs.db
.cache
.project
.settings
.tmproj
*.esproj
nbproject
*.sublime-project
*.sublime-workspace


# ------------------------------------------------------------------------------
# Dreamweaver added files
# ------------------------------------------------------------------------------

_notes
dwsync.xml


# ------------------------------------------------------------------------------
# Komodo
# ------------------------------------------------------------------------------

*.komodoproject
.komodotools


# ------------------------------------------------------------------------------
# Add your custom excludes below
# ------------------------------------------------------------------------------
50 changes: 50 additions & 0 deletions .jshintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
{
"predef": [
"jasmine",
"spyOn",
"it",
"console",
"describe",
"beforeEach",
"waits",
"waitsFor",
"runs",
"alert",
"confirm",
"Modernizr",
"impress",
"exports",
"self",
"define",
"google"
],

"node" : true,
"es5" : false,
"browser" : true,
"jquery": true,

"boss" : false,
"curly": false,
"debug": false,
"devel": false,
"eqeqeq": true,
"evil": true,
"forin": false,
"immed": false,
"laxbreak": true,
"laxcomma": true,
"newcap": true,
"noarg": true,
"noempty": false,
"nonew": false,
"nomen": false,
"onevar": false,
"plusplus": false,
"regexp": false,
"undef": true,
"sub": true,
"strict": false,
"white": false,
"asi": true
}
9 changes: 9 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
The MIT License

Copyright (c) 2014- Nick Baugh <[email protected]> (http://niftylettuce.com/)

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
177 changes: 177 additions & 0 deletions Readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,177 @@

# Express Paginate

Node.js pagination middleware and view helpers.

To be used in combination with database pagination plugins such as [mongoose-paginate](https://github.com/edwardhotchkiss/mongoose-paginate).

**This module was created namely for use with [Eskimo](http://eskimo.io)**



## Install

```bash
npm install -S express-paginate
```



## API

```js
var Paginate = require('express-paginate')
var paginate = new Paginate(10, 50)
```


### paginate

This creates a new instance of `express-paginate` to be used with all routes or specific route(s).

#### Arguments

* `limit` a Number to limit results returned per page (defaults to `10`)
* `maxLimit` a Number to restrict the number of results returned to per page (defaults to `50`) &ndash; through this, users will not be able to override this limit (e.g. they can't pass `?limit=10000` and crash your server)


### paginate.middleware(req, res, next)

This middleware validates and supplies default values to `req.query.limit`, `req.query.page`, `res.locals.paginate`, `res.locals.hasPreviousPages`, and `res.locals.hasNextPages`.

### Arguments

* `req` (**required**) &ndash; the request object returned from Express middleware invocation
* `res` (**required**) &ndash; the response object returned from Express middleware invocation
* `next` (**required**) &ndash; the next object returned from Express middleware invocation


### paginate.href(req)

When you use the `paginate` middleware, it injects a view helper function called `paginate.href` as `res.locals.paginate`, which you can use in your views for paginated hyperlinks (e.g. as the `href` in `<a>Prev</a>` or `<a>Next</a>`).

By default, the view helper `paginate.href` is already executed with the inherited `req` variable, therefore it becomes a function capable of returning a String when executed.

When executed with `req`, it will return a function with one argument called `prev`.

This argument `prev` is a Boolean and is completely optional (defaults to `false`).

Pass `true` as the value for `prev` when you want to create a `<button>` or `<a>` that points to the previous page (e.g. it would generate a URL such as the one in the `href` attribute of `<a href="/users?page=1&limit=10">Prev</a>` if `req.query.page` is `2`).

[See the example below for an example of how implementation looks](#example).

#### Arguments

* `req` (**required**) &ndash; the request object returned from Express middleware invocation

#### Returned function arguments when invoked with `req`

* `prev` (optional) &ndash; a Boolean to determine whether or not to increment the hyperlink returned by `1` (e.g. for "Next" page links)


### paginate.hasPreviousPages

When you use the `paginate` middleware, it injects a view helper Boolean called `hasPreviousPages` as `res.locals.hasPreviousPages`, which you can use in your views for generating pagination `<a>`'s or `<button>`'s &ndash; this utilizes `req.query.page > 1` to determine the Boolean's resulting value (representing if the query has a previous page of results)


### paginate.hasNextPages(req)

When you use the `paginate` middleware, it injects a view helper function called `hasNextPages` as `res.locals.hasPreviousPages`, which you can use in your views for generating pagination `<a>`'s or `<button>`'s &ndash; if the function is executed, it returns a Boolean value (representing if the query has another page of results)

By default, the view helper `paginate.hasNextPages` is already executed with the inherited `req` variable, therefore it becomes a function capable of returning a Boolean when executed.

When executed with `req`, it will return a function that accepts two required arguments called `pageCount` and `resultsCount`.

#### Arguments

* `req` (**required**) &ndash; the request object returned from Express middleware invocation

#### Returned function arguments when invoked with `req`

* `pageCount` (**required**) &ndash; a Number representing the total number of pages for the given query executed on the page



## Example

```js

// # app.js

var express = require('express')
var paginate = require('express-paginate')

// keep this before all routes that will use pagination
app.use(paginate.middleware(10, 50))

app.get('/users', function(req, res, next) {

// This example assumes you've previously defined `Users`
// as `var Users = db.model('Users')` if you are using `mongoose`
// and that you've added the Mongoose plugin `mongoose-paginate`
// to the Users model via `User.plugin(require('mongoose-paginate'))`
Users.paginate({}, req.query.page, req.query.limit, function(err, pageCount, users, itemCount) {

if (err) return next(err)

res.format({
html: function() {
res.render('users', {
users: users,
pageCount: pageCount,
itemCount: itemCount
})
},
json: function() {
// inspired by Stripe's API response for list objects
res.json({
object: 'list',
has_more: paginate.hasNextPages(req)(pageCount),
data: users
})
}
})

})

})

var app = express()
app.listen(3000)
```

```jade
//- users.jade
h1 Users
ul
each user in users
li= user.email
include _paginate
```

```jade
//- _paginate.jade
//- This examples makes use of Bootstrap 3.x pagination classes
if (paginate.hasPreviousPages || paginate.hasNextPages(pageCount)
.navigation.well-sm#pagination
ul.pager
if previous
li.previous
a(href=paginate.href(true)).prev
i.fa.fa-arrow-circle-left
| Previous
if next
li.next
a(href=paginate.href()).next
| Next&nbsp;
i.fa.fa-arrow-circle-right
```


Loading

0 comments on commit ec00677

Please sign in to comment.