Search code examples
javascriptangularjsrestrestangular

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

Solution

  • 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.'));
          });
      };