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"
    }
  ]
}

  

 

转载于:https://www.cnblogs.com/shenggen/p/6402523.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值