Skip to content

Instantly share code, notes, and snippets.

@portokallidis
Last active January 28, 2016 20:39
Show Gist options
  • Save portokallidis/d49ffa04284a5c959646 to your computer and use it in GitHub Desktop.
Save portokallidis/d49ffa04284a5c959646 to your computer and use it in GitHub Desktop.
Pagination and Filter Configuration Extra for ng-admin+loopback API server
// Pagination and Filter Configuration Extra for ng-admin+loopback API server
// Loopback.io API Mapping
app.config(['RestangularProvider', function (RestangularProvider) {
function Get(yourUrl){
var Httpreq = new XMLHttpRequest();
Httpreq.open("GET",yourUrl,false);
Httpreq.send(null);
return Httpreq.responseText;
};
RestangularProvider.addResponseInterceptor(function(data, operation, what, url, response, deferred) {
if (operation === 'getList') {
var Result = JSON.parse(Get(url + '/count'));
response.totalCount = Result.count;
}
return data;
});
// use the custom query parameters function to format the API request correctly
RestangularProvider.addFullRequestInterceptor(function(element, operation, what, url, headers, params) {
if (operation === "getList") {
// custom pagination params
if (params._page) {
params['filter[limit]'] = params._perPage;
params['filter[skip]'] = (params._page - 1) * params._perPage; //skip is the same os offset
delete params._page;
delete params._perPage;
}
// custom sort params
if (params._sortField) {
params['filter[order]']=params._sortField + ' '+ params._sortDir;
delete params._sortDir;
delete params._sortField;
}
return {params: params};
}
});
}]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment