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 ...
随机推荐
- plsql 查询到别的用户下面的表
原因:勾选了"Allow multiple connections" 解决方法:打开"Tools"下面的"Perference",里面有个& ...
- C语言函数sscanf()的用法(转)
转自:http://www.cnblogs.com/lyq105/archive/2009/11/28/1612677.html C语言函数sscanf()的用法 sscanf() - 从一个字符串中 ...
- Buffer ByteBuffer 缓冲区
http://blog.sina.com.cn/s/blog_4150f50c0100gfa3.html
- 查看与修改网关,DNS
网关是网络中的路由器,作为访问其他网络的接入点. 修改ip地址 即时生效: ifconfig eth0 192.168.0.20 netmask 255.255.255.0 启动生效: 修改/etc/ ...
- ActiveMQ 无法启动 提示端口被占用 解决方案
http://bob-zhangyong.blog.163.com/blog/static/17610982012729113326153/ ————————————————————————————— ...
- Meter and pixel units in a box2d game - LibGDX
http://pimentoso.blogspot.com/2013/01/meter-and-pixel-units-in-box2d-game.html 需FQ ————————————————— ...
- windows下GVIM的配置(vimrc)
学习python时想要在gvim中配置python的编译环境,网上找到一个比较好用的vimrc配置,保存下来以备下次有需要. set encoding=utf-8 set termencoding=u ...
- Windows的静态库使用步骤
windows库程序: 1.静态库程序 - 运行时不独立存在,会被链接到可执行文件或者动态库中,目标程序的归档. 文件扩展名:LIB 2.动态库程序 - 运行时独立存在,不会被链接到可执行文件或其他动 ...
- MemoryStream类读写内存
和FileStream一样,MemoryStream和BufferedStream都派生自基类Stream,因此它们有很多共同的属性和方法,但是每一个类都有自己独特的用法.这两个类都是实现对内存进行数 ...
- Material design之Views and Shadows
Views and Shadows: elevation是构成控件阴影的基本属性.通过设置较高的Z值可以接受更大的阴影,阴影只能投射到Z=0的平面上. View Elevation 控件的Z值,是由两 ...