将PUT添加到AngularJS中的默认NG资源操作

Dev*_*xon 5 javascript angularjs angularjs-resource

我正在尝试将PUT添加到ng-resource中的默认方法.到目前为止,我将DEFAULT_ACTIONS修改为:

var DEFAULT_ACTIONS = {
      'get':    {method:'GET'},
      'save':   {method:'POST'},
      'update':   {method:'PUT'},
      'query':  {method:'GET', isArray:true},
      'remove': {method:'DELETE'},
      'delete': {method:'DELETE'}
    };
Run Code Online (Sandbox Code Playgroud)

但这感觉非常hacky,当我更新模块时显然不会持久.有没有办法可以将更新/放置添加到所有将持续更新的ng-resource对象?

rob*_*ert 11

另一种选择是配置$ resourceProvider.这将对ALL $资源生效,您也需要在测试中包含此代码,最有可能.

// Config the $resourceProvider
app.config(["$resourceProvider",function ($resourceProvider) {

  // extend the default actions
  angular.extend($resourceProvider.defaults.actions,{

    // put your defaults here
    query : {
      method : "GET",
      isArray : false,
      transformResponse : function (data) {
        // my data is wrapped in an object under the property "results"
        return angular.fromJson(data).results;
      }
    }

  });
}]);
Run Code Online (Sandbox Code Playgroud)


Ila*_*mer 6

我能看到的唯一简单方法是在$ resource周围创建一个包装器:

module.factory('$myResource', ['$resource', function($resource){
  return function(url, paramDefaults, actions){
     var MY_ACTIONS = {
       'update':   {method:'PUT'}
     };
     actions = angular.extend({}, MY_ACTIONS , actions);
     return $resource(url, paramDefaults, actions);
  }
}]);
Run Code Online (Sandbox Code Playgroud)