相关模型的分页结果
Paginate Result of related models
这是对之前 . There are only two models involved: category
and game
which share a hasMany
relation. Example: The endpoint /Categories/1001/games/mature
list all games of fighting
category that have mature
flag set to true
. However, I am unable to paginate
回复的又一次跟进。根据下面显示的代码进行分页的正确方法是什么?我想一次只显示 10 个结果。
common/models/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'
}
}
);
Tables/Models
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
}
.....
]
如果你被上面的代码卡住了,你需要下载全套游戏然后在前端分页。如果无法将 limit
和 skip
值发送到您的查询,则别无他法。
如果您可以更改此代码并向远程方法添加参数,则带有节点 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);
});
应将 limit 和 skip 的值添加到您的远程方法定义和注册中,然后 UI 可以根据显示的页面发送动态值。
跳过过滤器上的页面也有分页示例:
https://docs.strongloop.com/display/public/LB/Skip+filter
如果这将使用某种 UI 库,例如 Angular SDK,您可以使用 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.
获得相同的结果
这是对之前 category
and game
which share a hasMany
relation. Example: The endpoint /Categories/1001/games/mature
list all games of fighting
category that have mature
flag set to true
. However, I am unable to paginate
回复的又一次跟进。根据下面显示的代码进行分页的正确方法是什么?我想一次只显示 10 个结果。
common/models/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'
}
}
);
Tables/Models
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
}
.....
]
如果你被上面的代码卡住了,你需要下载全套游戏然后在前端分页。如果无法将 limit
和 skip
值发送到您的查询,则别无他法。
如果您可以更改此代码并向远程方法添加参数,则带有节点 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);
});
应将 limit 和 skip 的值添加到您的远程方法定义和注册中,然后 UI 可以根据显示的页面发送动态值。
跳过过滤器上的页面也有分页示例:
https://docs.strongloop.com/display/public/LB/Skip+filter
如果这将使用某种 UI 库,例如 Angular SDK,您可以使用 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.