-
Notifications
You must be signed in to change notification settings - Fork 840
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Restangular caches attributes #811
Comments
This is incorrect. Nowhere in the Restangular source is I also tried this myself just now (via the JS console) and confirmed that attributes modified immediately before a call to save (aka PUT) is made are passed through. Maybe if you included a little more context we could spot the problem? |
I created a very small application that simulates the problem here: https://github.com/daveroberts/restangular_test The instructions to run the code are in the README file, but here are the highlights. The application is a simple user edit form. The form lets you change the person's name: <div ng-controller="person_edit_controller">
<h1>Edit {{person.name}}</h1>
<form>
<div>Name: <input type="text" ng-model="person.name" /></div>
<button ng-click="save(person)">Save</button>
</form>
</div> The user is retrieved with the following json: {"id":1,"name":"Jon"} After clicking save, the person's name is changed to "Hard coded", and hobby_ids are set onto the person: var app = angular.module('myapp', ['restangular']);
app.controller('person_edit_controller', function($scope, Restangular){
$scope.person = Restangular.one('people', 1).get().$object;
$scope.save = function(person){
person.name = "Hard coded";
person.hobby_ids = [1,2,3];
person.save();
};
}); Restangule submits a PUT request to /people/1 as expected, but the parameters that are sent an incorrect:
The expected result is that the person's name is sent as "Hard coded", and hobby_ids are included. |
Ah, you're right! Looks like it's related to issue #55 but specific to You can work around this bug in the meanwhile by changing your controller code to: Restangular.one('people', 1).get().then(function(data) {
$scope.person = data;
});
``1 |
Thank you, that work around seems to work. For now we will avoid using $object, and look forward to a bug fix in a future release. One more question: The params which are sent to the server are in this format: As you can see, the parameters are sent twice, and the hobby_ids are only sent at the "root" level. Is it possible to adjust how the parameters are sent so that they are not sent twice? Is it possible to adjust what the root element should be (or if there should be one at all?) |
Sorry mate. Been a long day at work. Pretty sure this confusion is just arising from Rails' logging. Watching Chrome's Network tab I see the following request data going out to {
"id":1,
"name":"Jon!!!!",
"hobby_ids":[1,2,3]
} The Rails log shows the following: Parameters: {"id"=>"1", "name"=>"Jon!!!!", "hobby_ids"=>[1, 2, 3], "person"=>{"id"=>"1", "name"=>"Jon!!!!"}} So the |
@bvaughn thanks for the help :). @daveroberts can you move the sample to a JSFiddle or plunkr so that it's easier to debug please? It does seem a bug indeed with For the API, you can use |
JS Fiddle: http://jsfiddle.net/daveroberts/xur4dqsp/ See the screenshot below of the error in action. It shows the chrome debugger's network tab for the put request to the server. As stated above, I expect the application to send the hard coded name and the hobby_ids array. Instead, the original, cached values of the todo are sent. |
I'm suffering from the same problem also. $object doesn't work as expected. This is kind of a blocker for me. I cant believe it hasn't come up more often. |
@Climax777 : There's a workaround. Use the promise instead. Not working: $scope.thing = Restangular.one('things', 7).get().$object; Working: Restangular.one('things', 7).get().then(function(t){ |
Adding to this thread. We are having issues ONLY on top level objects. For instance: Anytime device.settings.power or device.settings.value changes, a save works fine, but if device.name changes, it seems to only send the original data. It is in fact fixed with the workaround mentioned above. |
Still an issue for me - workaround by Restangular.copy on element before saving, but I'd much appreciate a real fix. |
In combination with #697 (Cloning sets |
I can confirm that by using $object, you can not simply do a put(). It will send the old object. When using the .then(success, failure) approach, everything works fine. |
I'm not sure if that is the same issue. I'm using angular.copy. The copy is modified properly - at least checking via console.log before saving - but the request payload contains old data. |
@akomm there's a section in the docs about using
|
I updated the fiddle (it was using Underscore instead of Lodash). I can also confirm the bug after playing around with the fiddle. If I have some time, I'll see if I can track down the problem. |
Closing as a duplicate of #579. Tracking changes there… |
I have a save function where I want to change / add properties before save:
Neither the new name, nor the hobby_ids is placed into the PUT request for the person. At the start of the save function, person did have a name, but not hobby_ids. The old name was placed into the PUT request.
The text was updated successfully, but these errors were encountered: