Как добавить перехватчики к вызову $resource
?
Скажем, у меня есть ресурс factory, называемый Users
, например:
app.factory('Users', ['$resource', 'resourceInterceptor',
function ($resource, resourceInterceptor) {
return $resource(
'users/:user_id',
{
user_id: '@id'
},
{
query: {
method: 'GET', // Not changing the default method, just adding an interceptor
interceptor: resourceInterceptor // Is there any better way to do this.. like globally?
},
save: {
method: 'POST', // Same here
interceptor: resourceInterceptor // Again...
},
..., // And so on
}
);
}]);
и моя служба resourceInterceptor
выглядит следующим образом:
app.factory('resourceInterceptor', ['$rootScope',
function ($rootScope) {
return {
request: function () {
// This function isn't executed at all?
$rootScope.loading = true;
},
response: function () {
$rootScope.loading = false;
},
responseError: function () {
$rootScope.loading = false;
}
};
}]);
Прежде всего, функция перехвата request
никогда не выполняется, почему бы и нет?
Во-вторых, необходимость жесткого кодирования перехватчика в существующих методах $resource
очень утомительна, есть ли способ упростить назначение перехватчиков определенным вызовам $resource
или, возможно, даже назначить перехватчик для всех вызовов $resource
?