-
Notifications
You must be signed in to change notification settings - Fork 27.4k
Issue with interceptors in $resource #11201
Comments
@Avcajaraville hard to say without a reproduce scenario... Could you put minimal code into plunker or similar. We are missing essential info here: AngularJS version used, exact interceptor code etc. |
Im using angular 1.3.11. The interceptor code is not very important I think, but here it is :
We need to add this hardcoded-url to every object on the response (debugging purpose while our team finish the api). As pointed on the stackoverflow answer, seems angular makes this on purpose. Also, comparing both objects when using interceptors (this way):
Is returning true, so I guess they have the same reference, thus, not causing memory issues. But still, its a weird behavior. I want to know why this happen and, if its expected at all or not. |
function defaultResponseInterceptor(response) {
return response.resource;
} This default interceptor is used only if there is no response interceptor specified by the user. $resource('...', {...}, {
someMethod: {
...
interceptor: {
response: function (response) {
var res = response.resource;
// Do stuff with `res`
return res;
}
}
}
}); |
@Avcajaraville I hope that @gkalpak answer covers your pb. |
@pkozlowski-opensource @gkalpak Ok, thanks for the answer then ! It really helps, the only thing I still missing is a brief answer about the "why", but know that is an expected behavior of angular makes me feel more comfortable about it. |
@Avcajaraville, I will gladly explain the "why" if I understand the question :) |
@gkalpak Why when using a custom interceptor, returning response will automatically add a resource with resoruce.data property ? We already got it on response.data. |
The default interceptor won't add any property (the property is already there, added by How
|
@gkalpak Perfect answer ! Thanks so much ! BTW; I have answer my own question on SO and credit you -> http://stackoverflow.com/questions/28748053/angularjs-issue-with-interceptors-in-resource/28812722#28812722 |
I don't understand how is it that this default interceptor is no documented anywhere, not in the $http documentation and not in the $resource documentation. Is it documented elsewhere? Am I missing something? |
(BTW, there's been some discussion on #9334 (comment) and linked issues.) |
Im having a weird behavior manipulating a response through $resource with a custom interceptor.
If I use a response interceptor like this :
Then, from my controller :
At this point, this is the output of the console.log( response ) :
On the response object, I have the data object, as expected, but I also have this resource : Resource object which also includes the response data again.
But, if I dont use a interceptor at all; Im getting the expected response :
I dont understand this behavior and Im concern about perform or memory issues.
Can somebody clarify this ?
PS: I need to use interceptors, because I have to modify the response from server.
Original question on stackoverflow:
http://stackoverflow.com/questions/28748053/angularjs-issue-with-interceptors-in-resource/
The text was updated successfully, but these errors were encountered: