Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RestAngular: put and customPUT are sending old object, not updated one

When I call put or customPUT and check the request body in the browser, the original object is sent, not the updated one. I checked the RestAngular object with a {{ object }} and it is being updated.

Here is the controller (APIUsers is a RestAngular Service):

$scope.objects = {};
(function waitForNgInit(fnct) {
    $scope.$watch('userID', function(newVal) {
        if (newVal !== undefined) {
            fnct();
        }
    });
})(function retrieveUser() {
    $scope.objects.user = APIUsers.one($scope.userID).get().$object;
});

$scope.saveSettings = function() {
    $scope.objects.user.customPUT($scope.objects.user).then(function(resp) {
        $scope.errors = [];
    }, function(err) {
        console.log(err.data);
        $scope.errors = err.data.errors;
    });
};

Here is the Jade (HTML Template Code):

div.user-settings.form-section(ng-controller="userSettingsFormController")
    {{ objects }}
    ul.error-box(ng-show="errors != undefined && errors.legnth != 0")
        li(ng-repeat="error in errors") {{ error.msg }}
    .form-group
        label(for="username") Username
        input(type="textfield" ng-model="objects.user.username")
    .form-group
        label(for="username") Email
        input(type="textfield" ng-model="objects.user.email")
    .form-group
        label(for="username") Profile Picture
        input(type="file" ng-model="objects.user.profilePicture")
    span.submit-button(ng-click="saveSettings()") Save Changes
like image 987
sinθ Avatar asked Feb 13 '23 15:02

sinθ


2 Answers

It is an old and known Restangular bug, see it at https://github.com/mgonto/restangular/issues/713

To make this work in the meanwhile is fixed (it seems that are working to fix it), you can use the code below:

$scope.submit = function submit() {
    Restangular
      .copy($scope.ledger)
      .save()
      .then(function () {
        growl.success(gettext('Updated successfully.'));
      });
  };
like image 194
Diosney Avatar answered Feb 15 '23 04:02

Diosney


I had the same problem. I think you need to wait for the digest cycle to complete for the underlying restangular object to update.

Try wrapping your put() behind a $timeout call:

$timeout(function () {
  $scope.objects.user.customPUT($scope.objects.user).then(function(resp) {
    $scope.errors = [];
  }, function(err) {
    console.log(err.data);
    $scope.errors = err.data.errors;
  });
});
like image 35
Sastopher Avatar answered Feb 15 '23 06:02

Sastopher