Permalink
Browse files

first commit

  • Loading branch information...
Physalix
Physalix committed Sep 23, 2012
0 parents commit 4ba7d1f2d2b0627becb69efa2a46097097ff13ed
Showing with 172 additions and 0 deletions.
  1. +73 −0 README.md
  2. +70 −0 index.js
  3. +29 −0 package.json
@@ -0,0 +1,73 @@
+# phx-pagination
+
+> ExpressJS + Mongoose ORM (ExpressJS/NodeJS/MongoDB) Document Query Pagination
+
+## Installation
+
+```bash
+$ npm install phx-pagination
+```
+
+## Usage
+
+```javascript
+
+/**
+ * basic example usage of `phx-pagination`
+ **/
+
+require('phx-pagination').init(app);
+
+/**
+ * Action wich display a country list page
+ */
+app.get('/', function (req, res) {
+ var page = req.param('page') || 1;
+ Country.paginate({limit: 15, page: page}, function (error, countries) {
+ if (error) {
+ console.error(error);
+ } else {
+ res.render('countries/index', { title: 'Country List', countries: countries });
+ }
+ });
+});
+
+/* EOF */
+
+```
+
+```html
+
+`Display <- preview / pages / next -> in the view`
+
+<div id="main">
+ <% countries.forEach(function (country) { %>
+ <div class="span-two-thirds">
+ country: <%= country.name %>
+ </div>
+ <div class="span-one-third">
+ <a href="/show/<%= country.id %>">Show</a>
+ <a href="/delete/<%= country.id %>" onclick="return confirm('Are you sure you want to delete?')">Delete</a>
+ </div>
+ <% }); %>
+ <%- paginate(countries) %>
+</div>
+
+/* EOF */
+
+```
+
+## Run Tests
+
+``` bash
+$ npm test
+```
+
+## Author: [Physalix][0]
+
+[0]: http://www.physalix.com/
+
+## License
+
+MIT
+
@@ -0,0 +1,70 @@
+/**
+ * Module dependencies
+ **/
+var mongoose = require('mongoose');
+
+/**
+ * Initialize pagination module
+ * @param {app} express app param
+ **/
+exports.init = function (app) {
+ var helpers = {};
+ helpers['paginate'] = function (req, res) {
+ return paginateHelper;
+ };
+ app.dynamicHelpers(helpers);
+};
+
+/**
+ * @function paginateHelper
+ * @param {opts} query Mongoose options
+ * @param {callback} callback
+ * Global view helper
+ **/
+function paginateHelper(collection) {
+ if (!collection.totalPages || collection.totalPages < 2) return '';
+ var page = parseInt(collection.currentPage, 10);
+ var pages = collection.totalPages;
+ var html = '<div class="pagination">';
+ var prevClass = 'prev' + (page === 1 ? ' disabled': '');
+ var nextClass = 'next' + (page === pages ? ' disabled': '');
+ var prevLink = ((page - 1) > 0 ? "?page="+(page - 1) : '#');
+ var nextLink = ((page + 1) <= pages ? "?page="+(page + 1) : '#');
+ html += '<ul><li class="' + prevClass + '">';
+ html += "<a href='" + prevLink + "'>&larr; Previous</a>";
+ html += '</li>';
+ for (var i = 1; i <= pages; i++ ) {
+ if (i == page) {
+ html += '<li class="active"><a href="#">'+i+'</a></li>';
+ } else {
+ html += "<li><a href='?page="+i+"'>"+i+"</a></li>";
+ }
+ }
+ html += '<li class="' + nextClass + '">';
+ html += "<a href='" + nextLink + "'>Next &rarr;</a>";
+ html += '</li></ul></div>';
+ return html;
+};
+
+/**
+ * @method paginate
+ * @param {opts} query Mongoose options
+ * @param {callback} callback
+ * Extend Mongoose Models to paginate queries
+ **/
+mongoose.Model.paginate = function(opts, callback) {
+ var limit = opts.limit || 10;
+ var page = opts.page || 1;
+ var Model = this;
+
+ Model.count(function (err, totalRecords) {
+ var query = Model.find({}).skip((page - 1) * limit).limit(limit);
+ query.exec(function(error, records) {
+ if (err) return callback(err);
+ records.totalRecords = totalRecords;
+ records.currentPage = page;
+ records.totalPages = Math.ceil(totalRecords / limit);
+ callback(null, records);
+ });
+ });
+}
@@ -0,0 +1,29 @@
+{
+ "name": "phx-pagination",
+ "description": "ExpressJS + Mongoose ORM (ExpressJS/NodeJS/MongoDB) Document Query Pagination",
+ "author": {
+ "name": "Physalix",
+ "email": "fs@physalix.com"
+ },
+ "repository": {
+ "type": "git",
+ "url": "https://github.com/physalix/phx-pagination.git"
+ },
+ "keywords": [
+ "paginate",
+ "pagination",
+ "mongoose",
+ "express"
+ ],
+ "version": "0.0.1",
+ "main": "index.js",
+ "engines": {
+ "node": ">= 0.4.0"
+ ,"mongoose": ">= 2.4.9"
+ },
+ "license": "MIT",
+ "dependencies": {
+ "mongoose": ">= 2.4.9"
+ },
+ "devDependencies": {}
+}

0 comments on commit 4ba7d1f

Please sign in to comment.