如何将“RKPaginator”与自定义“页面参数”一起使用?我的API不提供“页面”参数,它使用这样的模式进行分页:
“http://my.domain.com/api/lists/?limit=5&offset=10”
“offset”是下一页的参数。
我的API的JSON返回表格如下所示:
"meta": {
"next": "/api/lists/?limit=5&offset=15",
"total_count": 22,
"previous": "/api/lists/?limit=5&offset=5",
"limit": 5,
"offset": 10
},
或者更容易定制tastypie来处理这样的请求? “http://my.domain.com/api/lists/?page=1” 我要覆盖哪些方法?
非常感谢。
答案 0 :(得分:4)
使用objective-c类别覆盖RKPaginator :: loadPage方法,并计算页码和限制的偏移量,如以下代码行所示:
#import "RKPaginator+Tastypie.h"
#import <objc/runtime.h>
@implementation RKPaginator (Tastypie)
- (void)TastypieLoadPage:(NSUInteger)pageNumber
{
[self TastypieLoadPage:(pageNumber-1) * self.perPage];
}
+ (void)load {
method_exchangeImplementations(class_getInstanceMethod(self, @selector(loadPage:)), class_getInstanceMethod(self, @selector(TastypieLoadPage:)));
}
@end
这里是RKPaginator对象的构造:
RKPaginator *paginator = [[RKObjectManager sharedManager] paginatorWithPathPattern:@"yourResource/?limit=:perPage&offset=:currentPage"];