Skip to content
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

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

Closed
AnatoliyLitinskiy opened this issue May 15, 2014 · 60 comments
Closed

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

AnatoliyLitinskiy opened this issue May 15, 2014 · 60 comments
Labels

Comments

@AnatoliyLitinskiy
Copy link

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 AnatoliyLitinskiy changed the title put method doesn't work as expected for .$object $object.put() method doesn't work as expected May 15, 2014
@svycka
Copy link
Contributor

svycka commented May 15, 2014

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

@AnatoliyLitinskiy
Copy link
Author

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

@danharper
Copy link

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.

@mgonto
Copy link
Owner

mgonto commented May 30, 2014

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

@AnatoliyLitinskiy
Copy link
Author

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.

@danharper
Copy link

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

@mgonto
Copy link
Owner

mgonto commented May 31, 2014

Thanks!!

I'll check it out :).

@windevalley
Copy link

can this merge with #579.

@henyana
Copy link

henyana commented Aug 4, 2014

Any solution regarding this bug?

@henyana
Copy link

henyana commented Aug 4, 2014

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

@kaiqigong
Copy link

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?

@ADmad
Copy link

ADmad commented Sep 8, 2014

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

@jusopi
Copy link

jusopi commented Oct 13, 2014

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
Copy link

jusopi commented Oct 13, 2014

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.

@zhuangya
Copy link

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

@gonzofish
Copy link

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

@chillyistkult
Copy link

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.

@picitujeromanov
Copy link

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

filledValue is loosing reference to data

@jwmay2012
Copy link

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.

@michikono
Copy link

+1

@thedig
Copy link

thedig commented Feb 12, 2015

Yup, same issue here.

@adamwlarson
Copy link

Copy definitely works, is there any traction on an actual fix for this issue? This had me stuck for quite a while before I found this issue.

@ADmad
Copy link

ADmad commented Feb 26, 2015

Given the severity of the issue and lack of action for quite a long time, I wonder if the project is still being actively supported / developed.

Looking at the commit history of master branch I don't see any commit by the original author @mgonto in months. Though I do some see some recent actively on couple of branches by @grabbou.

Would be nice if the community was updated on the status of the project so that just in case the author / contributors of the project are not longer able to maintain it people can move on.

@grabbou
Copy link
Contributor

grabbou commented Feb 26, 2015

@ADmad - I am working with @mgonto to fix all the bugs as soon as possible. He's not available at the moment to maintain the project actively as he's giving few talks (e.g. ng-conf). We are in touch all the time having weekly Hangouts so we are pretty on the way to release 1.5 soon.

New release should come out in ~2 weeks with most bugs fixed. We have actually reduced number of opened issues from 180 to around 110 which is a great progress. There are still few PRs waiting to be merged which takes some time as they've been done ages ago so they need some extra loving in order to make them master ready.

@garhbod
Copy link

garhbod commented Mar 23, 2015

Thanks @gonzofish I did work it out in the end.

But your example is very helpful and would be nice to something like it in the documentation.

@gonzofish
Copy link

No problem @garhbod the only problem is that the isServer variable is specific to my data...that is, I know that only the data coming from the server could have an ID field on it...while this is probably common for a lot of projects, it isn't necessarily universal, which makes the docs a little less straightforward to write.

@mmichaelbiz
Copy link

@mgonto, do you have any update on if this will be soon fixed? Was it included in the recent versions?

PUT requests are somewhat important... :)

garhbod pushed a commit to garhbod/restangular that referenced this issue Apr 13, 2015
@bmonty
Copy link

bmonty commented May 19, 2015

+1

@tgbarker
Copy link

+1, any news on this?

@tomasgarzon
Copy link

+1, any news?

@blaues0cke
Copy link

+1, but the using Restangular.copy instead of angular.copy is a working workaround for me.

@Fauny
Copy link

Fauny commented Jul 13, 2015

+1
When to fix?????

@arthurspa
Copy link

+1

@zhuangya
Copy link

i'm unsubscribing this thread.

tired of +1s but no solution.

here is my way to wrap RESTful api for angular:


return $resource(BACKEND_URL, {parameter: '@parameter'}, {/** custom methods **/})

you can build a factory, let's say API, return something like:

return {
  users: { //assuming you have a api called users like i do
    return $resource(USER_API_URL, { username: '@username', id: '@id'}, {
      login: {
        method: 'POST',
        url: LOGIN_USER_OVERRIDING,
        param: {
          action: 'login'
        },
        responseType: 'json',

      }
    });

  }
};

and i think this is way much easier..

@picitujeromanov
Copy link

Tired of this as well. We switched to restmod instead.

Dňa 20 Jul 2015, o 06:45, Ya Zhuang [email protected] napísal:

i'm unsubscribing this thread.

tired of +1s but no solution.

here is my way to wrap RESTful api for angular:

return $resource(BACKEND_URL, {parameter: '@parameter'}, {/** custom methods **/})

you can build a factory, let's say API, return something like:

return {
users: { //assuming you have a api called users like i do
}
};

and i think this is way much easier..
—
Reply to this email directly or view it on GitHub.

@zhuangya
Copy link

hmm.. looks good to me, worth trying

@garhbod
Copy link

garhbod commented Jul 21, 2015

I've created a solution. I don't know why it hasn't being pulled yet

@arthurspa
Copy link

@picitujeromanov, thak you for the suggestion. In few minutes I migrated from restangular, that was with this bug, to restmod and now my app is working fine.

@picitujeromanov
Copy link

You're welcome

Dňa 21 Jul 2015, o 12:50, Arthur Silva [email protected] napísal:

@picitujeromanov, thak you for the suggestion. In few minutes I migrated from restangular, that was with this bug, to restmod and now my app is working fine.


Reply to this email directly or view it on GitHub.

@gonzofish
Copy link

i'm going back to $http & $q...

@YannDoW
Copy link

YannDoW commented Oct 12, 2015

I had the same problem. Using:

Restangular.copy(resourceToUpdate).save();

solved the issue. Thanks to the community!

@quarryman
Copy link

Restangular.copy(resourceToUpdate).save() makes PUT request all the time however save should distinguish between POST and PUT

@simbeto
Copy link

simbeto commented Dec 5, 2015

looks like project is kind of abandoned!
This seems to be like an important bug fix.

@dflourusso
Copy link

Restangular v1.5.1

Same problem for me, using .then(), it works fine.

@hartz89
Copy link

hartz89 commented Mar 28, 2016

Hey all,

I discovered that (at least in my case) this is actually not a bug with Restangular. My problem was that I was using angular.copy() instead of Restangular.copy() to copy an element into a scope prior to editing it. I have a feeling many of you may have been doing the same, since this is common (and usually good) practice in Angular. This IS pointed in the documentation, but I have been missing it up until now...

https://github.com/mgonto/restangular#copying-elements

Before modifying an object, we sometimes want to copy it and then modify the copied object. We can't use angular.copy for this because it'll not change the this binded in the functions we add to the object. In this cases, you must use Restangular.copy(fromElement).

I hope this helps!

@daviesgeek
Copy link
Collaborator

Closing as a duplicate of #579. Tracking changes there…

@deleugpn
Copy link

Had to settle for the Restangular.copy(vm.record).save() solution. Frankly, I don't follow what @hartz89 mean by that last comment, but I know that I just passed the object to a component (<whatever record="$restangularObject"></whatever>) and inside that component I have a ng-model pointed to the object.

The interesting thing is that if I console.log(vm.record) RIGHT BEFORE vm.record.save() I can see the data changed, but the PUT send the wrong data.

@daviesgeek
Copy link
Collaborator

@deleugpn The comment was pointing out the fact that you need to copy an element using Restangular.copy or calling clone() on the element in question, not angular.copy or something else. Are you copying the object before calling save?

@deleugpn
Copy link

@daviesgeek I don't understand Angular enough to answer that. All I know is that I pass the object from one Angular Component to another through a scope variable scope: { object: "=" }.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests