node.js---sails项目开发(6)--- 实现分页功能
只需要添加一个文件即可 api/blueprints/find.js 代码如下
/**
* Module dependencies
*/
var util = require('util'),
actionUtil = require('../../node_modules/sails/lib/hooks/blueprints/actionUtil'); /**
* Find Records
*
* get /:modelIdentity
* * /:modelIdentity/find
*
* An API call to find and return model instances from the data adapter
* using the specified criteria. If an id was specified, just the instance
* with that unique id will be returned.
*
* Optional:
* @param {Object} where - the find criteria (passed directly to the ORM)
* @param {Integer} limit - the maximum number of records to send back (useful for pagination)
* @param {Integer} skip - the number of records to skip (useful for pagination)
* @param {String} sort - the order of returned records, e.g. `name ASC` or `age DESC`
* @param {String} callback - default jsonp callback param (i.e. the name of the js function returned)
*/ module.exports = function findRecords (req, res) { // Look up the model
var Model = actionUtil.parseModel(req); // If an `id` param was specified, use the findOne blueprint action
// to grab the particular instance with its primary key === the value
// of the `id` param. (mainly here for compatibility for 0.9, where
// there was no separate `findOne` action)
if ( actionUtil.parsePk(req) ) {
return require('./findOne')(req,res);
} // Lookup for records that match the specified criteria
var query = Model.find()
.where( actionUtil.parseCriteria(req) )
.limit( actionUtil.parseLimit(req) )
.skip( actionUtil.parseSkip(req) )
.sort( actionUtil.parseSort(req) ); // TODO: .populateEach(req.options);
query = actionUtil.populateRequest(query, req);
query.exec(function found(err, matchingRecords) {
if (err) return res.serverError(err); // Only `.watch()` for new instances of the model if
// `autoWatch` is enabled.
if (req._sails.hooks.pubsub && req.isSocket) {
Model.subscribe(req, matchingRecords);
if (req.options.autoWatch) { Model.watch(req); }
// Also subscribe to instances of all associated models
_.each(matchingRecords, function (record) {
actionUtil.subscribeDeep(req, record);
});
} // get pagination info and wrap results in struct var metaInfo,
criteria = actionUtil.parseCriteria(req),
skip = actionUtil.parseSkip(req),
limit = actionUtil.parseLimit(req); Model.count(criteria)
.exec(function(err, total){
if (err) return res.serverError(err); metaInfo = {
start : skip,
end : skip + limit,
limit : limit,
total : total,
criteria: criteria
}; res.ok({info: metaInfo, items: matchingRecords}); });
});
};
重写路由(/:modelIdentity/find)
查看效果,请输入如下链接, 详情 请看我的github https://github.com/shenggen1987/sails-demo.git
http://localhost:1337/order/find?limit=1&skip=1
{
"info": {
"start": 1,
"end": 2,
"limit": 1,
"total": 2,
"criteria": {}
},
"items": [
{
"name": "000",
"age": 92,
"phone": "15268155415",
"createdAt": "2017-02-14T09:15:08.242Z",
"updatedAt": "2017-02-14T09:15:08.242Z",
"id": "58a2ca9c8a7b1e1500883405"
}
]
}
node.js---sails项目开发(6)--- 实现分页功能的更多相关文章
- Node JS后端项目开发与生产环境总结
原文地址:Node JS后端项目开发与生产环境总结 Node JS常用后端框架有express.koa.sails.国产框架有个egg js,已经在cnode投入生产了,还有个think js,类似t ...
- 前端(Node.js)(3)-- Node.js实战项目开发:“技术问答”
1.Web 与 Node.js 相关技术介绍 1.1.Web应用的基本组件 web应用的三大部分 brower(GUI)<==>webserver(business logic.data ...
- Node.js 从零开发 web server博客项目[express重构博客项目]
web server博客项目 Node.js 从零开发 web server博客项目[项目介绍] Node.js 从零开发 web server博客项目[接口] Node.js 从零开发 web se ...
- Node.js 从零开发 web server博客项目[数据存储]
web server博客项目 Node.js 从零开发 web server博客项目[项目介绍] Node.js 从零开发 web server博客项目[接口] Node.js 从零开发 web se ...
- Node.js 从零开发 web server博客项目[koa2重构博客项目]
web server博客项目 Node.js 从零开发 web server博客项目[项目介绍] Node.js 从零开发 web server博客项目[接口] Node.js 从零开发 web se ...
- Node.js 从零开发 web server博客项目[安全]
web server博客项目 Node.js 从零开发 web server博客项目[项目介绍] Node.js 从零开发 web server博客项目[接口] Node.js 从零开发 web se ...
- Node.js 从零开发 web server博客项目[日志]
web server博客项目 Node.js 从零开发 web server博客项目[项目介绍] Node.js 从零开发 web server博客项目[接口] Node.js 从零开发 web se ...
- Node.js 从零开发 web server博客项目[登录]
web server博客项目 Node.js 从零开发 web server博客项目[项目介绍] Node.js 从零开发 web server博客项目[接口] Node.js 从零开发 web se ...
- Node.js 从零开发 web server博客项目[接口]
web server博客项目 Node.js 从零开发 web server博客项目[项目介绍] Node.js 从零开发 web server博客项目[接口] Node.js 从零开发 web se ...
- Node.js 从零开发 web server博客项目[项目介绍]
web server博客项目 Node.js 从零开发 web server博客项目[项目介绍] Node.js 从零开发 web server博客项目[接口] Node.js 从零开发 web se ...
随机推荐
- O(1)取Queue中的最大值
实现原理: 1.利用Stack的先进后出的特性,实现一个MaxStack,MaxStack中用一个Stack记录当前的值,一个Stack记录当前的最大值. 2.用2个MaxStack实现MaxQueu ...
- ios-A+B经典问题
// // main.m // a+b // #import <Foundation/Foundation.h> #import "Calcultor.h" int m ...
- ansible分发密钥
http://www.361way.com/ansible-cfg/4401.html 修改host_key_checking(默认是check的):改为false, host_key_ch ...
- SVN 提交出错:Attempted to lock an already-locked dir
http://www.2cto.com/kf/201306/221414.html —————————————————————————————————————————————————————— 在ec ...
- 技巧C#
1. 在CallBack之后保持滚动条的位置: 在Asp.Net1.1中,CallBack之后保持滚动条的位置是一件非常痛苦的事情,特别是页中有一个Grid并且想要编辑特定的行.为了不停留在想 ...
- php 合并数组的方法 非array_merge
Array ( [0] => Array ( [max] => 50 [date] => 2016-01-07 ) [1] => Array ( [max] => 100 ...
- OpenWrt 安装usb支持
(一)下载软件 1)komd-usb-ohci kmod-usb2 kmod-usb-storage kmod-usb-core 这些是USB驱动包 2) kmod-nls-base kmod-nls ...
- webservice linux 杀进程
一 . linux 下首先查看进程 ./stopServcer.sh server1如果停不掉websphere服务.则强制杀掉其进程! 1.查看websphere进程号bejqylc06:~ # ...
- 【转】MFC OnIdle的详细说明
转载出处:http://blog.csdn.net/tsing_best/article/details/25055707 CWinApp::OnIdlevirtual BOOL OnIdle( LO ...
- win 8升级win8.1的几个问题
对于习惯了win7来说的小朋友,win8的使用简直有种想砸电脑的冲动啊,特别是玩游戏的小朋友,win8各种bug.对于游戏.咳咳,我只是多说了,我们都是爱学习的好孩子,少玩游戏,少撸.简单说一 ...