2015-11-26 34 views
1

所以我想實現與帆更直截了當分頁和而不是使用當前頁或剛剛頁面跳轉......帆API:藍圖分頁 - 自定義實現

所以,我已經實現了自定義查找操作,取代默認的一個... 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)); 
    } 

的問題是,我得到頁面查詢參數(帆搜索參數)...我想從參數

列表中刪除

所以我想是這樣的:

var page = req.param("page", null); 
if(page){ 
    console.log(page); 
    delete req.query.page; 
} 

但帆仍然術語網頁搜索..

什麼是正確的方式做到這一點?

+0

我發現有一個黑名單定義在帆qhen解析parayes req.options.criteria.blacklist ...所以只是將它添加在那裏..我認爲這將解決它 –

回答

0

我有同樣的問題,我解決它感謝你的代碼,加入一兩件事情:

  1. 在你的代碼中調用parseCriteria之前初始化黑名單,加入「頁」字作爲如下:

    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) { 
        ... 
    

這將避免使用「頁」蛞蝓作爲其中的條件。

  • 創建藍圖模塊中的自定義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; 
    }; 
    
  • 這將允許避免瞭如果句子和離開的代碼,因爲它是。所以,你的代碼的等價最後部分將如下:

    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) { 
          ... 
    

    我希望它可以幫助別人!