You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository has been archived by the owner on Apr 12, 2024. It is now read-only.
First, here's a reproduced test case and a bit of explanation what's going on: https://gist.github.com/880088
(Run those 3 files as an app engine app for an easy way to host it that will allow POSTing to the .json file.)
Create a $resource class, then .get() it. Use that data bound to the page to create a little form. In that form, a button that will call .save() on the same resource. However: in the callback passed to .get() I'm accessing a property of the resource, also an object, storing a reference to it, and modifying it.
This modification is an oversimplification of what I'm doing in my real app. There, I am fetching two separate $resources: questions and answers. I cross-reference them together in the constructor (e.g. this.question[i].answer = this.answers[j] -- but this also a bit oversimplified). Then when rendering the page I can access this.question and this.question.answer, both to know what sort of field to render, and what value to pre-populate in it.
So what I'm seeing is that my view is bound to a question, the question has a reference to the answer, but after I .save() the answers, those objects are thrown away and the resource now contains a brand new set of answer objects, constructed from the response of the save call.
So early on, I had my server simply echo back the object that was just saved, and things appeared to work. It was only later that I discovered the whole cross-reference-breaking behavior which surfaces subtle bugs in our system.
Via the test case linked at the top, this is revealed in that the "obj.value" is 42 in the resource, set to -1 in the controller and otherwise not touched -- but .save()ing the resource re-loads that data from the server and overwrites the -1 value.
I don't want save() to load data from the server, I want it write data to the server. I want it to read the model, not write to it. Doing this breaks my app.
The text was updated successfully, but these errors were encountered:
You can debate the particulars of this implementation, but I can confirm that this change resolves the behavior described above. A .save() does not mutate the existing resource instance, and thus does not break the test case as described, and my real app too.
First, here's a reproduced test case and a bit of explanation what's going on:
https://gist.github.com/880088
(Run those 3 files as an app engine app for an easy way to host it that will allow POSTing to the .json file.)
Create a $resource class, then .get() it. Use that data bound to the page to create a little form. In that form, a button that will call .save() on the same resource. However: in the callback passed to .get() I'm accessing a property of the resource, also an object, storing a reference to it, and modifying it.
This modification is an oversimplification of what I'm doing in my real app. There, I am fetching two separate $resources: questions and answers. I cross-reference them together in the constructor (e.g. this.question[i].answer = this.answers[j] -- but this also a bit oversimplified). Then when rendering the page I can access this.question and this.question.answer, both to know what sort of field to render, and what value to pre-populate in it.
So what I'm seeing is that my view is bound to a question, the question has a reference to the answer, but after I .save() the answers, those objects are thrown away and the resource now contains a brand new set of answer objects, constructed from the response of the save call.
So early on, I had my server simply echo back the object that was just saved, and things appeared to work. It was only later that I discovered the whole cross-reference-breaking behavior which surfaces subtle bugs in our system.
Via the test case linked at the top, this is revealed in that the "obj.value" is 42 in the resource, set to -1 in the controller and otherwise not touched -- but .save()ing the resource re-loads that data from the server and overwrites the -1 value.
I don't want save() to load data from the server, I want it write data to the server. I want it to read the model, not write to it. Doing this breaks my app.
The text was updated successfully, but these errors were encountered: