restangular icon indicating copy to clipboard operation
restangular copied to clipboard

$object.put() method doesn't work as expected

Open AnatoliyLitinskiy opened this issue 12 years ago • 60 comments

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?

AnatoliyLitinskiy avatar May 15 '14 15:05 AnatoliyLitinskiy

You are just to fast. You have to use .then() here.

svycka avatar May 15 '14 15:05 svycka

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(); });

AnatoliyLitinskiy avatar May 15 '14 17:05 AnatoliyLitinskiy

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.

danharper avatar May 18 '14 19:05 danharper

@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!

mgonto avatar May 30 '14 01:05 mgonto

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.

AnatoliyLitinskiy avatar May 30 '14 10:05 AnatoliyLitinskiy

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"}';

danharper avatar May 30 '14 10:05 danharper

Thanks!!

I'll check it out :).

mgonto avatar May 31 '14 22:05 mgonto

can this merge with #579.

windevalley avatar Jun 05 '14 06:06 windevalley

Any solution regarding this bug?

henyana avatar Aug 04 '14 15:08 henyana

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 :)

henyana avatar Aug 04 '14 17:08 henyana

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?

kaiqigong avatar Aug 15 '14 09:08 kaiqigong

I am experiencing the same issue. Copying the object and then saving does work and stated in above comments.

ADmad avatar Sep 08 '14 09:09 ADmad

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 });

jusopi avatar Oct 13 '14 15:10 jusopi

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.

jusopi avatar Oct 13 '14 18:10 jusopi

it seems that, after Restangular.copy(), .save() send every request via PUT

zhuangya avatar Nov 13 '14 07:11 zhuangya

If I Restangular.restangularizeElement the element before I .save() it PUTs the new data...might not be ideal, but it works.

gonzofish avatar Nov 15 '14 15:11 gonzofish

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.

chillyistkult avatar Nov 28 '14 22:11 chillyistkult

problem is on this line https://github.com/mgonto/restangular/blob/master/src/restangular.js#L885

filledValue is loosing reference to data

picitujeromanov avatar Jan 27 '15 17:01 picitujeromanov

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.

jwmay2012 avatar Feb 04 '15 17:02 jwmay2012

+1

michikono avatar Feb 12 '15 19:02 michikono