How do I use an interceptor
within an Angular $resource
?
My JSON structure:
var dgs = [{id :1,
driver:'Sam',
type: 'bus',
segments:[{id:1,origin:'the bakery',arrival:'the store'},
{id:2,origin:'the store' ,arrival:'somewhere'}]
},
{ ... },
{ ... }
];
My controller is the following:
function dgCtrl($scope,$http,DriveGroup,Segment) {
$scope.dgs = DriveGroup.query(function()
// Code below may belong in a response interceptor?
for (var i=0;i<$scope.dgs.length;i++) {
var segments = $scope.dgs[i].segments;
for (var j=0;j<segments.length;j++) {
segments[j] = new Segment(segments[j]);
}
}
});
My services, and what I tried using the interceptor
object:
angular.module('dgService',['ngResource']).
factory("DriveGroup",function($resource) {
return $resource(
'/path/dgs',
{},
{update:{method:'PUT'})
{fetch :{method:'GET',
// This is what I tried.
interceptor:{
response:function(data) {
console.log('response',data);
},
responseError:function(data) {
console.log('error',data);
}
},
isArray:true
}
);
});
I read $resource, and it seems like this should work, but it doesn't, so I'm mis-understanding. Any suggestions?
How do I use an interceptor
within an Angular $resource
?
My JSON structure:
var dgs = [{id :1,
driver:'Sam',
type: 'bus',
segments:[{id:1,origin:'the bakery',arrival:'the store'},
{id:2,origin:'the store' ,arrival:'somewhere'}]
},
{ ... },
{ ... }
];
My controller is the following:
function dgCtrl($scope,$http,DriveGroup,Segment) {
$scope.dgs = DriveGroup.query(function()
// Code below may belong in a response interceptor?
for (var i=0;i<$scope.dgs.length;i++) {
var segments = $scope.dgs[i].segments;
for (var j=0;j<segments.length;j++) {
segments[j] = new Segment(segments[j]);
}
}
});
My services, and what I tried using the interceptor
object:
angular.module('dgService',['ngResource']).
factory("DriveGroup",function($resource) {
return $resource(
'/path/dgs',
{},
{update:{method:'PUT'})
{fetch :{method:'GET',
// This is what I tried.
interceptor:{
response:function(data) {
console.log('response',data);
},
responseError:function(data) {
console.log('error',data);
}
},
isArray:true
}
);
});
I read $resource, and it seems like this should work, but it doesn't, so I'm mis-understanding. Any suggestions?
Share Improve this question edited Oct 9, 2013 at 12:57 Shikiryu 10.2k9 gold badges52 silver badges76 bronze badges asked Sep 3, 2013 at 1:47 AlfredAlfred 1111 silver badge6 bronze badges1 Answer
Reset to default 6Your service is improperly formed. There are curly braces and parentheses int the wrong places.
Here is the correct version (modified slightly so that I could get it to run: http://jsfiddle/roadprophet/VwS2t/
angular.module('dgService', ['ngResource']).
factory("DriveGroup", function ($resource) {
return $resource(
'/', {}, {
update: {
method: 'PUT'
},
fetch: {
method: 'GET',
// This is what I tried.
interceptor: {
response: function (data) {
console.log('response in interceptor', data);
},
responseError: function (data) {
console.log('error in interceptor', data);
}
},
isArray: false
}
}
);
});