-
Notifications
You must be signed in to change notification settings - Fork 27.4k
TransformRequest in $http is changing scope #12468
Comments
Can you show us what you're doing with TransformData? Thanks. |
Are you saying "this is changing scopes without triggering a digest and that's bad"? I'm not quite sure what you're saying with this one. |
changes made in TransformRequest are changing the scope in my view, because of an $digest cycle, and this is bad. Which TransformData? |
@Gregoirevda I cannot reproduce your example: http://plnkr.co/edit/l44pMzhehCxm1of2xj7X?p=preview |
Here is a Plunker showing the problem: I managed to NOT change the scope inside the transformRequest by making a copy of the data parameter. transformRequest : function(data){ I found it strange a copy isn't done automaticaly, if passed by value OR reference. |
I think this is a real bug. The transformResponse fn makes a copy of the response, so the whole thing is cacheable. The only problem is a certain performance overhead, I assume. |
On the other hand ... if you are transforming your data, shouldn't you know where this data is currently referenced and then make a copy yourself in the transformer? Copying by default is not the best idea, since our copy isn't equipped for all special cases (although the same cases might be an issue for toJson). It might also be a problem for large payloads. |
I believe that we should not make the copy by default. In general it is bad practice to mutate an object that has been passed to you. Since you don't know who else relies upon that object. So I would expect that transformRequest functions to take responsibility for copying an object if they intend to mutate it. @Narretz I don't suppose you have time to update the |
@petebacondarwin That shouldn't be a problem - I'll update the docs. |
I thought it was strange I hadn't access to a angular.copy() of the data in the transform functions. Because the purpose of the functions (Request and Response) is to have a filter when data is going in and out of the application, to keep data IN and OUT separate. This way I can work with 2 data structures and have immediate access to the good structured data. If, when data goes OUT, the filter affects also application data (IN)... it makes no sense. |
Think of filters as being pure functions. They should have no side effects, which includes mutating the properties of the data passed in. |
Thanks @Narretz |
I'm sending scope data trough $http and change that data with TransformRequest before it reaches the API.
When the data is send, an $digest cycle occurs and changes also the scope data.
Note that there is a watcher on scope.value
Here is a basic example of how I change the data to send:
scope that is send:
Is this a normal behaviour?
Is there a way to transform the data I send trough http, but keep the scope as it is?
When debugging in chrome, I see the scope is changed when this piece of angularJS code is triggered:
I set /* Comments */ to comment code
The text was updated successfully, but these errors were encountered: