【问题标题】:Adding PUT to default NG-Resource Actions in AngularJS将 PUT 添加到 AngularJS 中的默认 NG-Resource 操作
【发布时间】:2014-01-23 00:53:15
【问题描述】:

我正在尝试将 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'}
    };

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

【问题讨论】:

    标签: javascript angularjs angularjs-resource


    【解决方案1】:

    另一个选项是配置 $resourceProvider。这将对所有 $resource 生效,您很可能也需要在测试中包含此代码。

    // 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;
          }
        }
    
      });
    }]);
    

    【讨论】:

      【解决方案2】:

      我能看到的唯一简单方法是围绕 $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);
        }
      }]);
      

      【讨论】:

      • 然后使用 $myResouce 而不是 $resource,我假设?
      • 是的,然后使用 $myResource,但 API 保持不变
      • @Magne 我能想象的唯一原因是关于使用哪种 HTTP 方法进行更新的冲突(PUT vs PATCH
      【解决方案3】:

      在你的应用配置函数中,自定义 $resourceProvider 来添加你自己的一组http操作参数,就像这样:

          angular.module('app')
                 .config(configureResourceProvider);
      
          function configureResourceProvider($resourceProvider){
      
          // Provide your own set of actions on $resource factory.
          // The following comments are Angular's default actions which are being
          // replaced by your customized set that includes a PUT method.
          //{ 'get':    {method:'GET'},
          //  'save':   {method:'POST'},
          //  'query':  {method:'GET', isArray:true},
          //  'remove': {method:'DELETE'},
          //  'delete': {method:'DELETE'} };
      
          $resourceProvider.defaults.actions = {
              create: {method: 'POST'},
              save:   {method: 'POST'},
              update: {method: 'PUT'},
              get:    {method: 'GET'},
              query:  {method: 'GET', isArray:true},
              remove: {method: 'DELETE'},
              delete: {method: 'DELETE'}
          };
      
          // Of course, you can customize other parameters too, like: 
          // Don't strip trailing slashes from calculated URLs
          $resourceProvider.defaults.stripTrailingSlashes = false;
      }
      

      【讨论】:

        【解决方案4】:
        app.config([ "$resourceProvider", function($resourceProvider) {
        
          $resourceProvider.defaults.actions['update'] = { method: 'PUT', params: { id: '@id' }, isArray: false }
        
        }]);
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2016-08-02
          • 1970-01-01
          • 2020-04-28
          • 1970-01-01
          • 2017-01-21
          相关资源
          最近更新 更多