所以我想用sails实现更直接的分页,而不是跳过使用currentPage或只是页面......
所以,我已经实现了一个自定义查找操作,取代了默认操作... https://github.com/randallmeeker/SailsBluePrintActions
这个在响应
上填充附加信息对象这是来源 https://github.com/randallmeeker/SailsBluePrintActions/blob/master/pagination/inBody/find.js
所以我做了,我添加了这个
module.exports = function findRecords(req, res) {
// definde page if exists
var page = req.param("page", null);
....
if (page) {
query = Model.find()
.where(actionUtil.parseCriteria(req))
.paginate({
page: page,
limit: actionUtil.parseLimit(req)
})
.sort(actionUtil.parseSort(req));
} else {
query = Model.find()
.where(actionUtil.parseCriteria(req))
.limit(actionUtil.parseLimit(req))
.skip(actionUtil.parseSkip(req))
.sort(actionUtil.parseSort(req));
}
问题是,我将页面作为查询参数(sails搜索参数)......我想将其从参数列表中删除
所以我尝试了类似的东西:
var page = req.param("page", null);
if(page){
console.log(page);
delete req.query.page;
}
但是帆仍然按照术语页面进行搜索..
这样做的正确方法是什么?
答案 0 :(得分:0)
我遇到了同样的问题,由于您的代码,我通过添加以下内容解决了这个问题:
在您的代码中调用parseCriteria之前初始化黑名单,添加“page”字,如下所示:
module.exports = function findRecords(req, res) {
// define page if exists
var page = req.param("page");
....
// Allow customizable blacklist for params NOT to include as criteria.
req.options.criteria = req.options.criteria || {};
req.options.criteria.blacklist = req.options.criteria.blacklist || ['page', 'limit', 'skip', 'sort', 'populate'];
if (page) {
...
这将避免使用'page'slug作为where条件。
在蓝图模块中创建自定义parseSkip函数,如下所示:
/**
* parseSkip is a function that overrides the behavior of the original from actionUtil,
* to allow the case a param 'page' is set in the request / query string
* @param {Request} req
*/
function parseSkip(req) {
var DEFAULT_SKIP = 0;
var skip = DEFAULT_SKIP;
if (req.param('skip') || (typeof req.options.skip !== 'undefined'))
skip = +req.param('skip');
else if (req.param('page') || (typeof req.options._page !== 'undefined')) {
skip = req.param('page') * actionUtil.parseLimit(req) - actionUtil.parseLimit(req);
}
return skip;
};
这将允许避免if句子并保持代码原样。因此,代码的等效最终部分如下:
module.exports = function findRecords(req, res) {
...
// Allow customizable blacklist for params NOT to include as criteria.
req.options.criteria = req.options.criteria || {};
req.options.criteria.blacklist = req.options.criteria.blacklist || ['limit', 'page', 'skip', 'sort', 'populate', 'order'];
// Lookup for records that match the specified criteria
var query = Model.find()
.where(actionUtil.parseCriteria(req))
.limit(actionUtil.parseLimit(req))
.skip(parseSkip(req))
.sort(actionUtil.parseSort(req));
query.exec(function found(err, matchingRecords) {
...
我希望它可以帮助别人!