-
Notifications
You must be signed in to change notification settings - Fork 27.4k
Don't copy server response to resource value if they are already the same object #4508
Conversation
Thanks for the PR!
If you need to make changes to your pull request, you can update the commit with Thanks again for your help! |
…s the resource instance
….js into dont-copy-resource
I am wondering what the reason behind throwing an error in |
I'm sorry, but I wasn't able to verify your CLA signature. CLA signature is required for any code contributions to AngularJS. Please sign our CLA and ensure that the CLA signature email address and the email address in this PR's commits match. If you signed the CLA as a corporation, please let me know the company's name. Thanks a bunch! PS: If you signed the CLA in the past then most likely the email addresses don't match. Please sign the CLA again or update the email address in the commit of this PR. |
Hmm... thought I had done that previously but I just completed the CLA again. |
02dc2aa
to
fd2d6c0
Compare
cad9560
to
f294244
Compare
e8dc429
to
e83fab9
Compare
4dd5a20
to
998c61c
Compare
I just checked and the test in this PR already passes on master. This is because we are now using the |
EXPLANATION
An $http response interceptor may be used to compare the value sent to the server--available via
response.config.data
--to the value returned from the server--available viaresponse.data
--BEFORE logic inside$resource
copiesresponse.data
to the resource instance value. The logic of this response interceptor MAY, in some cases (see use case below), set the value ofresponse.data
toresponse.config.data
which, when using$resource
also happens to be a resource instance value.This very simple pull request simply skips
$resource
's attempt to copy theresponse.data
to the resource value if the two already reference the same object. Without this change, thecopy()
function will throw when called from within the resource.USE CASE
The use case leading to this pull request is as follows. In an application implementing auto-save,
$resource
is used to periodically persist in-progress changes (identified usingFormController.$dirty
) to the server. While that save request is outstanding, the user may continue to make changes. Because$resource
's default logic will discard any changes made between the time the save request was issued and it returns usingcopy(data, value);
, a response interceptor has been implemented with custom application logic to merge three states of the object being saved:The application's custom merge logic, to avoid firing unnecessary watches or losing the user's changes, merges states (1) and (3) onto the existing object which is
response.config.data
and then setsresponse.data
toresponse.config.data
. This currently causes$resource
to throw and requires the very minor customization in this pull request. I'd LOVE to NOT have a customized copy ofngResource
in my app.