Prevent ngResource from requiring a returned item on $save

后端 未结 2 945
栀梦
栀梦 2021-01-28 12:52

It seems that when I $save or use a custom $update (put or post) method on my resource that it expects the newly posted document to be returned. I don\'t want to return anything

相关标签:
2条回答
  • 2021-01-28 13:00

    When you do a $save() there will be a validation to check if there is any data returned and if thats the case, the response will be copied to the original object (in your case item)

    This is the callback when receiving a response from server:

    var promise = $http(httpConfig).then(function(response) {
      var data = response.data,
          promise = value.$promise;
    
      if (data) {
        // Need to convert action.isArray to boolean in case it is undefined
        // jshint -W018
        if (angular.isArray(data) !== (!!action.isArray)) {
          throw $resourceMinErr('badcfg', 'Error in resource configuration. Expected ' +
            'response to contain an {0} but got an {1}',
            action.isArray?'array':'object', angular.isArray(data)?'array':'object');
        }
        // jshint +W018
        if (action.isArray) {
          value.length = 0;
          forEach(data, function(item) {
            value.push(new Resource(item));
          });
        } else {
          shallowClearAndCopy(data, value);
          value.$promise = promise;
        }
      }
    
      value.$resolved = true;
    
      response.resource = value;
    
      return response;
    }, function(response) {
      value.$resolved = true;
    
      (error||noop)(response);
    
      return $q.reject(response);
    });
    

    You can check full source code here

    So if you don't want to have your object overwritten, then just don't send any data on the response from server.

    0 讨论(0)
  • 2021-01-28 13:12

    If your server returns a 204 (no-content), it won't replace the resource once the save is complete.

    0 讨论(0)
提交回复
热议问题