Skip to content

Commit

Permalink
pagination
Browse files Browse the repository at this point in the history
  • Loading branch information
melnx committed Jul 19, 2015
1 parent db13bdf commit aa27070
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 14 deletions.
60 changes: 53 additions & 7 deletions lib/Resource/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,7 @@ Resource.prototype.query = function( q , opts , complete ) {
}
var self = this;
var Model = self.Model;
console.log(opts);

async.waterfall([
executePreFunctions,
Expand All @@ -339,15 +340,60 @@ Resource.prototype.query = function( q , opts , complete ) {
});

function executeMethod( q , done ) {
var query = Model.find( q ).sort('-_id');
if (opts.populate) {
query.populate( opts.populate );

function execQuery(q, done, pageInfo){
var query = Model.find( q ).sort('-_id');

if(pageInfo){
if(pageInfo.skip){
query = query.skip( pageInfo.skip );
}
if(pageInfo.limit){
query = query.limit( pageInfo.limit );
}
}

if (opts.populate) {
query.populate( opts.populate );
}

query.exec(function(err, collection) {
if (err) return complete(err);
console.log("COLLECTION:");
console.log(collection);

if(pageInfo){
return done( err, {collection:collection, isArrayWrapper:true, pageInfo:pageInfo});
}else{
return done( err , collection );
}
});
}

query.exec(function(err, collection) {
if (err) return complete(err);
return done( err , collection );
});
if(opts.pagination && opts.pagination.usePagination){
var pagination = opts.pagination;

var pageSize = pagination.pageSize;
console.log("PAGINATION: ");
console.log(pagination);

Model.count( q ).exec(function(err, count){
if(count > pagination.pageSize){
var pageInfo = {
skip: pagination.pageSize * pagination.page,
limit: pageSize,
pageCount: count / pagination.pageSize,
page: pagination.page
};
execQuery(q, done, pageInfo);
}else{
execQuery(q, done, null);
}
});
}else{
console.log("PAGINATION OFF");
execQuery(q, done, null, null);
}
}

function executePreFunctions( done ) {
Expand Down
46 changes: 40 additions & 6 deletions lib/Service/http.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,14 @@ var HTTP = function() {

require('util').inherits( HTTP , Service );

function getPagination(req){
var pagination = {lol:'hi', usePagination:true, pageSize:1};
if(req.param('page')) {
pagination['page'] = parseInt( req.param('page') );
}
return pagination;
}

HTTP.prototype.pre = function( event , binding ) {
if (!~['query', 'get', 'update', 'delete', 'create'].indexOf( event )) {
throw new Error('event "'+event+'" is not a valid method.');
Expand Down Expand Up @@ -91,10 +99,14 @@ HTTP.prototype.provide = function() {

var resource = self.maki.resources[ r ];
resource.query( filter , {
populate: requirement.populate
populate: requirement.populate,
pagination: getPagination(req)
}, function(err, obj) {
var element = {};

console.log("OBJ");
console.log(obj);

if (requirement.single) {
element[ resource.names.get ] = (obj) ? obj[0] : {};
} else {
Expand Down Expand Up @@ -123,6 +135,10 @@ HTTP.prototype.provide = function() {

locals = _.merge( locals , always );

console.log("LOCALS:");
console.log(locals);
console.log("RENDERING:" + resource.templates[ req.m ]);

res.render( resource.templates[ req.m ] , locals , function( err , body , next ) {
if (self.maki.debug) console.log('render callback', err );
if (err && err.view && err.view.path) return res.render('error'); // TODO: replace with Error handler
Expand All @@ -142,11 +158,24 @@ HTTP.prototype.provide = function() {
var subject = data;

if (subject && !Array.isArray(subject)) {
return res.render('resource', _.merge( always , {
resource: resource,
item: subject
}) );
if(subject.isArrayWrapper){
console.log("RENDERING ARRAY WRAPPER");
var collection = subject.collection;
subject.collection = null;
console.log(resource);
return res.render('resource', _.merge( always, subject , {
resource: resource,
collection: collection
}) );
}else{
console.log("RENDERING SINGLE");
return res.render('resource', _.merge( always , {
resource: resource,
item: subject
}) );
}
} else {
console.log("RENDERING ARRAY");
return res.render('resource', _.merge( always , {
resource: resource,
collection: subject
Expand Down Expand Up @@ -596,9 +625,14 @@ HTTP.prototype.attach = function( maki ) {
var q = {};

resource[ p ]( q , {
populate: methodPopulationMap[ p ]
populate: methodPopulationMap[ p ],
pagination: getPagination(req)
}, function(err, instance) {
if (err) return res.error( err );

console.log("OBJ");
console.log(instance);

res.provide( resource.name , instance , handlers );
});
}
Expand Down
9 changes: 8 additions & 1 deletion views/modules/collection.jade
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,14 @@ table.ui.table.segment.sortable

tfoot
tr
td #{collection.length} items
td
if (pageInfo)
div.ui.pagination.menu
- var i = 0
while i < pageInfo.pageCount
a.active.item(href="/#{resource.plural.toLowerCase()}?page=#{i}")= i++
else
td #{collection.length} items

script.
$(window).load(function() {
Expand Down

0 comments on commit aa27070

Please sign in to comment.