$object.put() method doesn't work as expected
var oneReal = Restangular.one('company',5).get();
// after it will be loaded in controller $scope.company = oneReal.$object; // {id:5,name:'company 5'} $scope.company.name = "company 5 edited"; $scope.company.put(); // sends old name 'company 5' not 'company 5 edited' // Do I have to use method "then(c)" to get loaded object, why I can't use $object that I put in template?
You are just to fast. You have to use .then() here.
ok, can I use $scope.company.put(); when loading of this object will be resolved? var oneReal = Restangular.one('company',5).get();
$scope.company = oneReal.$object; // {id:5,name:'company 5'} like this: oneReal.then(function(element){ // why this doesn't work? object has same data and method put()! // but sends old, not changed data ! // this is my main question. $scope.company.name = "company 5 edited"; $scope.company.put();
// this works as expected. element.name = '... changed' element.put(); });
I'm having the same issue. I have a service which returns Restangular's get(id).$object. This is retrieved from the server & displayed into a form correctly. I then update the model:
$scope.model.name = 'Bar';
This all seems fine, however once I call $scope.model.save(), the original data is sent up.
Edit: I'm now just using .then() instead of .$object which works fine. Shame .$object doesn't work, though.
@AnatolLitnskiy @danharper thanks for the report. Can you please create a plunkr or jsfiddle of this not working so that I can test it out and debug it easier?
I'll mark this as a bug.
Thanks!
Hi but jsfiddle not completely suitable to repeat this. For example to get object from server Restangular uses GET method, while jsfiddle needs POST to /echo/json/ adress in this way we can't get needed data to make restangularized element. Restangular.all('echo').one('json').get(); will never work on jsfiddle
Here is my attempt to release it in jsfiddle http://jsfiddle.net/v96Py/1/ But it doesn't do at all what we need. It can't get restangularized object.
Here's an example: http://jsfiddle.net/v96Py/2/
As you can see, when using the .get().$object method, the model's name is changed from Dan to Bob, however the data sent to the server on .save() is the original data:
{"id":1,"name":"Dan"}
However, when the same code is run for the model using the .get().then(...) method, the correct, new, name is sent to the server:
{"id":1,"name":"Bob"}
The server-side code isn't important, it's just returning a dummy object:
<?php
header('Access-Control-Allow-Origin: *');
header('Content-Type: application/json');
echo '{"id": 1, "name": "Dan"}';
Thanks!!
I'll check it out :).
can this merge with #579.
Any solution regarding this bug?
I've just changed my code from
$scope.news = angular.copy(original);
to
$scope.news = Restangular.copy(original);
$scope.news.put().then(function(news){ ...
and now it works :)
I have met the same problem. I do not want to use then callback.
me = Restangular.one('users','me').get().$object
save = ()->
me.avatar = 'new avatar'
console.log me # the new avatar
me.put() # the avatar does not change (from network, I can see the avatar is still the old value)
@henyana I tried copy, and it worked, but the code looks ugly. : (
Is this by design or bug?
I am experiencing the same issue. Copying the object and then saving does work and stated in above comments.
I haven't tried the copy method but will do so shortly...
... but the promise method by @danharper over at http://jsfiddle.net/v96Py/2/ does not work in my implementation. The intuitive way I'd expect this to work would be like so:
//init data
var itemRqst = Restangular.one('foo').get()
itemRqst.then( function() { $scope.item = itemRqst.$object });
//save data
var saveRqst = $scope.item.put() //or .save() //should be sending modified data, not original.
saveRqst.then( function() { $scope.item = saveRqst.$object });
btw, the 'copy' fix doesn't seem to work for getList.
I tried digging into the addRequestInterceptor but the problem arises before making it to that stage as the element parameter has the original data.
it seems that, after Restangular.copy(), .save() send every request via PUT
If I Restangular.restangularizeElement the element before I .save() it PUTs the new data...might not be ideal, but it works.
Any progress on this bug?
Restangular.copy(object).save() works for me as a workaround, but just object.save() does not - old values are used in this case.
problem is on this line https://github.com/mgonto/restangular/blob/master/src/restangular.js#L885
filledValue is loosing reference to data
I'm also running into this problem. When I do this:
restObj.save().then((response)->
console.log(response)
)
The old version is sent off and console.log response shows the unedited original object.
(NOTE: The API responds with the saved object)
@chilly90's fix worked for me.
Restangular.copy(restObj).save().then((response) ->
console.log(response)
)
The new correct version is sent of and console.log(response) shows the proper new object.
+1