2015-12-14 24 views
0

这是另一个后续到之前的question。只涉及两种模式:categorygame,它们共享hasMany关系。示例:端点/Categories/1001/games/mature列出所有fighting类别的游戏,其中mature标志设置为true。但是,我无法对paginate作出回应。基于下面显示的代码分页的正确方法是什么?我想一次只显示10个结果。分页相关型号的结果

通用/模型/ category.js

Category.mature = function(id, callback) { 
    var app = this.app; 
    var Game = app.models.game; 
    Category.findById(id, {}, function(err, category) { 
     if (err) return callback(err); 
     //Now call the Game find method 
     Game.find({ 
      "where": { 
       categoryId: id, 
       mature: true 
      } 
     }, function(err, gameArr) { 
      if (err) return callback(err); 
      callback(null, gameArr); 
     }); 
    }); 
} 


Category.remoteMethod(
'mature', { 
    accepts: [{ 
     arg: 'id', 
     type: 'number', 
     required: true 
    }], 
    // mixing ':id' into the rest url allows $owner to be determined and used for access control 
    http: { 
     path: '/:id/games/mature', 
     verb: 'get' 
    }, 
    returns: { 
     arg: 'games', 
     type: 'array' 
    } 
} 
); 

表/型号

catgories 

category_name  category_id 
-------------  ----------- 
fighting   1001 
racing    1002 
sports    1003 

games 

game_id   game_name   category_id  mature  description   published_date 
-----------  ------------  -----------  -------  -----------   -------------- 
13KXZ74XL8M  Tekken    10001   true  Published by Namco.  1994 
138XZ5LPJgM  Forza    10002   false  Published by Microsoft 2005 

API结果:

games [ 
{ 
gameName: 'Tekken', 
gameInfo : 
[ 
    { 
     description : 'Published by Namco.', 
     published_date : '1994' 
    } 
], 
     categorName: 'fighting', 
     categoryId: 1001, 
     mature: true 
} 
..... 
] 

回答

2

如果你被卡住上面的代码,你需要下载t他全套游戏,然后在前端分页。如果无法将limitskip值发送给您的查询,则没有其他办法。

如果你可以改变这个代码,并添加参数的远程方法,与节点API底层的MySQL连接器的格式应该是这样的:

Game.find({ 
    "where": { 
    categoryId: id, 
    mature: true 
    }, 
    "limit": 10, // 10 per page 
    "skip": 10 // hard coded for page 2, this needs to be passed in 
}, function(err, gameArr) { 
    if (err) return callback(err); 
    callback(null, gameArr); 
}); 

值的极限,并跳过应添加到您的远程方法定义和注册,然后UI可以根据显示的页面发送动态值。

上跳过过滤器的页面有分页以及一个例子:如果这将是使用某种UI库,如角SDK的

https://docs.strongloop.com/display/public/LB/Skip+filter

,你可以在同一查询控制器级别使用lb-ng生成器脚本和在那里创建的模型。你也可以在那里添加分页值,不需要自定义的远程方法。

更新:

要跳过和限制号码添加到您的远程方法,你需要更新你的远程方法的签名中,accepts阵列将变为

accepts: [ 
    { 
    arg: 'id', 
    type: 'number', 
    required: true 
    }, 
    { 
    arg: 'skip', 
    type: 'number', 
    required: true 
    }, 
    { 
    arg: 'limit', 
    type: 'number', 
    required: true 
    } 
] 

然后添加相同的2新的论据方法本身:

Category.mature = function(id, skip, limit, callback) { 
    // ...your code... 
}); 

您可以在这一点上使用查询参数调用它,就像?skip=10&limit=10强制附加到现有路径。或者,您可以将http.path更改为类似

path: '/:id/games/mature/:skip/:limit' 

,然后调用资源与/1/games/mature/10/10使用参数化URL以达到同样的效果。

+0

有没有办法通过url将'skip'的值传递给远程方法? –

+0

是的,看我的更新。 – Brian

+0

是否可以使用'gameId'作为偏移来显示下一个'x'项目,而不是使用'skip'。有些人喜欢'/ 1/games/mature?nextid = 13KXZ74XL8M' –