RKPaginator Params

how can i use "RKPaginator" with custom page parameter? My API does not provide a "page" parameter, it uses a template like this for pagination:

" http://my.domain.com/api/lists/?limit=5&offset=10 "

"offset" is the parameter for the next page.

The JSON return form of my API looks like this:

"meta": {
    "next": "/api/lists/?limit=5&offset=15",
    "total_count": 22,
    "previous": "/api/lists/?limit=5&offset=5",
    "limit": 5,
    "offset": 10
},

      

Or would it be easier to set up tastypie to handle requests like this? " http://my.domain.com/api/lists/?page=1 " What methods should I override?

Many thanks.

+3


source to share


1 answer


Override the RKPaginator :: loadPage method with the objective-c category and calculate the offset from the page number and constraint as shown in the following lines of code:

#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

      



And here comes the construction of the RKPaginator object:

RKPaginator *paginator = [[RKObjectManager sharedManager] paginatorWithPathPattern:@"yourResource/?limit=:perPage&offset=:currentPage"];

      

+3


source







All Articles