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

putElement not working when using Restangular.copy() #728

Open
joelbowen opened this issue May 26, 2014 · 6 comments · May be fixed by #872
Open

putElement not working when using Restangular.copy() #728

joelbowen opened this issue May 26, 2014 · 6 comments · May be fixed by #872

Comments

@joelbowen
Copy link

Please let me know if I'm not doing this in proper fashion, but the following is happening for me with 1.3.x and 1.4.0:

Restangular.one('parent', parentId).all('childcollection').getList().then(function(collection) {
  $scope.childCollection = Restangular.copy(collection);
});

$scope.updateChildren = function(index) {
  $scope.childCollection.putElement(index).then(function(updatedCollection) {
    //Use updated collection    
  });
}

The above code returns an 'undefined is not a function' on putElement()

However if I do not use Restangular.copy but instead do:

Restangular.one('parent', parentId).all('childcollection').getList().then(function(collection) {
  $scope.childCollection = collection;
});

No problems using putElement.

@mgonto
Copy link
Owner

mgonto commented May 30, 2014

I don't really know what's going on but I'll check it out.

Can you please provide a jsfiddle or plunkr with the bug happening so that it's easy to debug it?

THanks!

@whitetrefoil
Copy link

I met a similar problem even w/o using .copy.
In my case, the collection in the then callback is wired because the new collection is NOT a restangularCollection:

Restangular.all('users').getList()
  .then(function(users) {
    users[0].name += 'a'
    users.putElement(0)
      .then(function(newUsers) {
        console.log(newUsers.restangularCollection); // => false
        console.log(newUsers.putElement); // => undefined

But my restangular has many configs like addResponseInterceptor. I'll try to work out a smallest fail case if I have time later.

@whitetrefoil
Copy link

This one can reproduce. The version is 1.4.0 with angular=1.2.23, both from bower.

<!DOCTYPE html>
<html lang='en-US' ng-app='app'>
<head>
  <meta charset='utf-8'>
  <title>Simplest</title>
  <script src='lib/lodash/dist/lodash.js'></script>
  <script src='lib/angular/angular.js'></script>
  <script src='lib/restangular/dist/restangular.js'></script>
  <script type="text/javascript">
  angular.module('app', ['restangular'])
    .controller('Ctrl', function($scope, Restangular) {
      Restangular.all('api').all('users').getList()
          .then(function (users) {
            $scope.users = users;
            $scope.test = function () {
              users[0].name = 'b';
              users.putElement(0).then(function(newUsers) {
                console.log(newUsers.restangularCollection);
                console.log(newUsers.putElement);
              })
            };
          });
      });
  </script>
</head>
<body ng-controller="Ctrl">
  <p>
    <button ng-click="test()">Test</button>
  </p>
</body>
</html>

whitetrefoil added a commit to whitetrefoil/restangular that referenced this issue Sep 11, 2014
@whitetrefoil
Copy link

Please checkout my test when you have time.
Hopefully I didn't misunderstand the expected function of putElement().
whitetrefoil@4d5c10f

@whitetrefoil whitetrefoil linked a pull request Sep 12, 2014 that will close this issue
@whitetrefoil
Copy link

Hi, I filed a PR includes the above tests and a possible fix.
But I'm not sure whether .restangularCollection() is proper or .restangularCollectionAndElements() is the right one.

@daviesgeek
Copy link
Collaborator

Any update on this, since 1.5.1 is now out?

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

Successfully merging a pull request may close this issue.

4 participants