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

PUT not putting changes when model is get through $object #579

Open
windevalley opened this issue Feb 18, 2014 · 24 comments
Open

PUT not putting changes when model is get through $object #579

windevalley opened this issue Feb 18, 2014 · 24 comments
Labels
Milestone

Comments

@windevalley
Copy link

restanglar v1.3.1

$scope.data = Restangular.one("collection", "1245").get().$object;
// I got back

{ 
  title: "some title"
}

$scope.data.title = "title 123";
$scope.data.put();

It will not put the changes.

But if get through promise it will work.

Restangular.one("collection", "1245").get().then(function(result) {
  $scope.data = result;
});
$scope.data.title = "title 123";
$scope.data.put(); // will work
@mgonto
Copy link
Owner

mgonto commented Feb 18, 2014

Hey,

Thanks for the report!

Can you provide a plunkr or jsfiddle with an example?

You can use http://angularjstalk.apiary.io/movies API as an example.

@mgonto mgonto added this to the 1.3.2 milestone Feb 18, 2014
@windevalley
Copy link
Author

@merlinran
Copy link
Contributor

I also faced same issue and created an example at http://jsfiddle.net/merlinran/8fvGh/
Same if I change put() to post().

http://jsonplaceholder.typicode.com/ is a great API for test.

@jondthompson
Copy link

Can this please get bumped? It's kind of a big bug.

@Quji
Copy link

Quji commented Oct 26, 2014

+1

8 similar comments
@mindmelting
Copy link

+1

@GarPit
Copy link

GarPit commented Oct 28, 2014

+1

@dslawrence
Copy link

+1

@diaakasem
Copy link

+1

@TheHandsomeCoder
Copy link

+1

@picitujeromanov
Copy link

+1

@deckchairlabs
Copy link

+1

@garhbod
Copy link

garhbod commented Mar 19, 2015

+1

@garhbod
Copy link

garhbod commented Mar 19, 2015

The fault is the following line 890

 _.extend(filledValue, data);

When the extend(or even merge) function is used to push the restangular response data into the filledValue it breaks the bindData for the put function.

maybe a callback function could be implemented to simply replace the empty object {} with the data rather than push it with extend

@garhbod
Copy link

garhbod commented Mar 19, 2015

To try and explain what I was trying to say here is two examples.

First if I manually do what $object does which results and the put containing the original data and not work.

    $scope.itemData = {};
    pagesRest.one(id).get().then(function(data) {
      angular.extend($scope.itemData, data);
    });

The Second is the same result on the front end but makes the save work.

    $scope.itemData = {};
    pagesRest.one(id).get().then(function(data) {
      $scope.itemData = data;
    });

garhbod pushed a commit to garhbod/restangular that referenced this issue Mar 20, 2015
@garhbod
Copy link

garhbod commented Mar 20, 2015

Someone with a bit more knowledge might come up with a better solution but my pull request #1091 fixed the problem for me.

Based off @gonzofish .restanularizeElement solution from issue #713

@peter-bertuglia
Copy link

Based on @garhbod's pull request, but without modifying restangular itself, I came up with this quick fix:

    .run(['Restangular', function (Restangular) {
      Restangular.addResponseInterceptor(function (data, operation, what, url, response, deferred) {
        var el = _.extend(deferred.promise.$object, data);
        return Restangular.restangularizeElement(data[Restangular.configuration.restangularFields.parentResource], el, what);
      });
    }])

@gonzofish
Copy link

@peter-bertuglia does that account for data that might be from the server but hasn't been Restangularized?

@peter-bertuglia
Copy link

@gonzofish I'm not sure what you mean by "might be from the server," this code is Restangularizing the object that is definitely coming from the server, it's intercepting the response

@gonzofish
Copy link

Sorry, my fault. I read the code the wrong way...thought it was a request interceptor...stupid me.

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

I am still experiencing the same issue, it looks like it's been there for a while now...
The "copy" method works but it adds quite some load on big objects.
Could there be a patch please ?

Truly yours, a coder in pain ;)

@victorsosa
Copy link

yet another +1

@saniaky
Copy link

saniaky commented Dec 8, 2016

+1

@ThomasCedrini
Copy link

ThomasCedrini commented Mar 7, 2017

@peter-bertuglia Great !! Here is an upgrade handling collections as well as element.

Restangular.addResponseInterceptor(function (data, operation, what, url, response, deferred) {
    var el = _.extend(deferred.promise.$object, data);

    if(angular.isArray(el))
	return Restangular.restangularizeCollection(data[Restangular.configuration.restangularFields.parentResource], el, what);

    return Restangular.restangularizeElement(data[Restangular.configuration.restangularFields.parentResource], el, what);
});

EDIT

There were too much codes.. this is well enough :

Restangular.addResponseInterceptor(function (data, operation, what, url, response, deferred) {
    return _.extend(deferred.promise.$object, data);
});

With the previous version, if you did a Restangular.extendModel, you received twice each resource instead of once. And when working with events, those were triggered twice..

Bye !

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