The save call with $ resource in angularjs does not pass complex objects

I'm having trouble passing complex objects to save for $ resource. It removes nested objects. Angular 1.2.6.

For example, I define a service:

services.factory(
    'DocumentService', 
    [
        '$resource',
        function($resource) {
            return  $resource(
                    '/app/rest/document/:id?user=:user', 
                    {
                        id: "@id", 
                        user: "@user"
                    }, 
                    {}
            );
        }
     ]
);

I go to the document service and retrieve the document.

It falls into the scope.
DS.get(
    {id : documentId, user: $scope.user},

    function(data) {
        $scope.document = data;
    }
);

I am adding an object meaning that the document has been edited:

$scope.document.documentEdit = {
    user : $scope.user,
    editAction : "update"
};

and another field:

$scope.document.testField = "this goes down";

When I call one of the following:

$scope.document.$save({id : documentId, user: $scope.user});
DS.save({id : documentId, user: $scope.user}, $scope.document);

"testField" is serialized and published, but not in the "documentEdit" field. I wonder if I do the following:

DS.save({id : documentId, user: $scope.user}, angular.toJson($scope.document));

It works; both fields are sent down.

What am I missing?

I took an Angular step in angular -resource.js. There is a piece of code:

var serverRequest = function(config) {

    headers = config.headers;
    var reqData = transformData(config.data, headersGetter(headers), config.transformRequest);

    // strip content-type if data is undefined
    if (isUndefined(config.data)) {
      forEach(headers, function(value, header) {
        if (lowercase(header) === 'content-type') {
            delete headers[header];
        }
      });
    }

    if (isUndefined(config.withCredentials) && !isUndefined(defaults.withCredentials)) {
      config.withCredentials = defaults.withCredentials;
    }

    // send request
    return sendReq(config, reqData, headers).then(transformResponse, transformResponse);
};

var chain = [serverRequest, undefined];
var promise = $q.when(config);

By the time it hits serverRequest, documentEdit has been filtered.

+3
source share

All Articles