Skip to content

Instantly share code, notes, and snippets.

@mamboer
Created March 11, 2015 10:01
Show Gist options
  • Save mamboer/d416af722a7f8c252fc0 to your computer and use it in GitHub Desktop.
Save mamboer/d416af722a7f8c252fc0 to your computer and use it in GitHub Desktop.
Loopback enhance, add pagination feature to the related scope object
// scope object pagination method
// reference : strong-remoting/lib/remote-objects.js line 550
// reference : strong-remoting/lib/shared-method.js line 245
define('__paginate__' + scopeName, {
isStatic: isStatic,
http: {verb: 'get', path: '/' + pathName+'/paginate'},
accepts: {arg: 'filter', type: 'object'},
description: 'Pagination Queries ' + scopeName + ' of ' + this.modelName + '.',
accessType: 'READ',
returns: {arg: scopeName, type: [toModelName], root: true}
},function(args,cbkFromSharedMethod){
//this will be referenced to the this.modelName's model instance
args = args || {};
var scopeMethod = this['__get__' + scopeName],
countMethod = this['__count__'+scopeName],
res = {total:0,items:[]},
me = this;
async.waterfall([
//get total count
function(callback){
countMethod.call(me,args.where,function(err,cnt){
if(err) return callback(err);
callback(null,cnt);
});
},
//get paginated items
function(total,callback){
if(total === 0){
return callback(null,res);
}
scopeMethod.call(me,args,function(err,items){
if(err) return callback(err);
res.total = total;
res.items = items;
callback(null,res);
});
}
],cbkFromSharedMethod);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment