这是我的整个拦截器,用于处理带有环回的分页、排序和过滤。希望它可以节省某人的时间。请注意,以“id”结尾的关系字段的过滤器使用相等处理,而其他字段的过滤器使用“like”。
myApp.config(['RestangularProvider', function (RestangularProvider) {
RestangularProvider.addFullRequestInterceptor(function(element, operation, what, url, headers, params) {
if (operation == "getList") {
// custom pagination params
if (params._page) {
params["filter[skip]"]= (params._page - 1) * params._perPage;
params["filter[limit]"] = params._perPage;
}
delete params._page;
delete params._perPage;
// custom sort params
if (params._sortField) {
params["filter[order]"] = params._sortField + " " + (params._sortDir || 'ASC');
delete params._sortField;
delete params._sortDir;
}
// custom filters
if (params._filters) {
var filterClause = "";
var i = 0;
for (var filter in params._filters) {
if (filter.endsWith('id')) {
params["filter[where][and][" + i + "][" + filter + "]"] = params._filters[filter];
} else {
params["filter[where][and][" + i + "][" + filter + "][like]"] = '%' + params._filters[filter] + '%';
}
i++;
}
delete params._filters;
}
}
return { params: params };
});
}]);