将分页游标和其他功能添加到端点结果 [英] Adding pagination cursor and additional features to endpoint result
问题描述
Strongloop api非常出色,可以直接使用.但是,我目前正在尝试进行一些自定义.我试图将一个远程方法添加到category
模型中,该方法将与以下端点数据一起返回:perPage
,total
,paging
(下面显示更多详细信息).对于paging
,before
和after
是基于gameId
的参数.如果值是null
或大于5
,则limit
将默认为5
.附加以得出前述结果的最佳方法是什么?
Strongloop api has been great to work with right out of the box. However I am currently trying to make some customizations. I am trying to add to the category
model a remote method that returns along with the endpoint data the following: perPage
, total
, paging
(shown below in more details). For paging
the before
and after
are parameters based on gameId
. limit
will default to 5
if value is null
or greater than 5
. What would be the best way to append to result the previously mentioned?
例如,http://localhost:3000/api/Categories/1004/games/mature?before=1000053
将返回所有小于1000053
的gameId
和before
所有大于1000053
的所有gameIds
.
For example, http://localhost:3000/api/Categories/1004/games/mature?before=1000053
will return all gameId
less than 1000053
and before
all gameIds
greater than 1000053
.
common/models/category.js
common/models/category.js
Category.findById(id, {}, function(err, category){
if (err) return callback(err);
//set limit
if (limit && limit > 5){
limit = 5;
}else if(limit === undefined){
limit = 5;
}
//set after cursor
Games.find({
"where": {
categoryId: id,
mature: true,
gameId: {gt: after}
},
"limit": limit
}, function(err, gameArr) {
if (err) return callback(err);
callback(null, gameArr);
});
});
/Categories/1004/games/mature
{
"perPage": 5, //value from limit
"total": 5, //total of return items from array
"data": [
... Endpoint data is here
],
"paging": {
"cursors": {
"after": 1000057, //last item in array
"before": 1000053 //first item in array
},
"previous": "http://localhost:3000/api/Categories/1004/games/mature?before=1000053" //url for previous
"next": "http://localhost:3000/api/Categories/1004/games/mature?after=1000057" //url for after
}
}
推荐答案
要将结果添加到响应中,请首先更改Category.mature远程方法返回给对象的响应类型:
To add results to the response, first change the type of response that the Category.mature remote method returns to an object:
Category.remoteMethod('mature', {
accepts: [
{arg: 'id', type: 'number', required: true},
{arg: 'limit',type: 'number',required: false},
{arg: 'after',type: 'number',required: false},
{arg: 'before',type: 'number',required: false}
],
// 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: 'object' // will be whatever you pass back as 2nd arg to callback()
}
});
然后仅使用现有的gamesArr作为data
的值,然后将要限制的值(在此之前和之后传入值)添加到新游戏对象中,作为第二个参数转到成功回调:
Then just add the values you want to the new games object, using your existing gamesArr as the value for data
, and the values passed in for the limit, before, and after values into the response object as the 2nd argument to the success callback:
callback(null, {
"perPage": limit,
"total": gameArray.length,
"data": gameArray,
"paging": {
"cursors": {
"after": gameArray[gameArray.length-1].game_id, // last game_id in result
"before": gameArray[0].game_id // first game_id in result
},
"previous": "http://localhost:3000/api/Categories/1004/games/mature?before=" + gameArray[0].game_id,
"next": "http://localhost:3000/api/Categories/1004/games/mature?after=" + gameArray[gameArray.length-1].game_id
}
})
正如我提到的有关使代码过于复杂的多个Games.find()调用一样,您也不应该删除该对象的3或4个副本.您可以将对象的构造包装在逻辑中,然后进行单个callback()调用,以简化远程方法的I/O管理并减少代码编写.减少到单个Games.find()调用将使发送此结果更加容易.
As I mentioned about the multiple Games.find() calls that were overcomplicating the code, you also shouldn't plunk down 3 or 4 copies of that object either. You can wrap the construction of the object in logic, then have a single callback() call to simplify the remote method's I/O management and write less code. Reducing to a single Games.find() call will make sending this result easier.
还要注意卷曲的撇号字符和制表符与空格的缩进(选择一个并坚持使用,不要混用),在精心构造和组织代码时,提供帮助要容易得多.
Also keep an eye out for curly apostrophe characters and tabs vs space indentation (pick one and stick with it, don't mix), it's much easier to help out when your code is constructed and organized with care.
这篇关于将分页游标和其他功能添加到端点结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!