如何使用AngularJS $资源的自定义操作? [英] How to use AngularJS $resource custom actions?

查看:89
本文介绍了如何使用AngularJS $资源的自定义操作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经用在几个仓库自定义操作。而到现在为止我只需要指定URL和方法。

I've been using custom actions in a few repositories. And up until now I only had to specify the url and the method.

例如:

updatePassword: {
  url: ENV.NITRO_PROJECT_REST_URL + '/admins/:adminId/password',
  method: 'PUT'
}

但后来,我不得不code,它有,而不是一个自定义操作,但有两个路径参数:

But then, I had to code a custom action that had, not one, but two path parameters:

technicianModule.controller('technician.teamCtrl',
  ['$scope', '$state', '$stateParams', 'CommonService', 'TechnicianService', 'TeamService', 'TeamTechnicianService',
  function($scope, $state, $stateParams, CommonService, TechnicianService, TeamService, TeamTechnicianService) {

    $scope.add = function(teamId) {
      TeamTechnicianService.add(teamId, $stateParams.technicianId, function() {
        TeamService.get(teamId, function(data) {
          $scope.teams.push(data);
          $scope.unassignedTeams.splice(CommonService.getResourceIndex($scope.unassignedTeams, data), 1);
        });
      });
    };

  }
]);

teamModule.factory('TeamTechnicianService',
  ['RESTService',
  function(RESTService) {
    var factory = {};

    factory.add = function(teamId, technicianId, callback) {
      return RESTService.TeamTechnician.add({teamId: teamId, technicianId: technicianId}).$promise.then(callback);
    }

    return factory;
  }
]);

所以我第一个codeD它像:

So I first coded it like:

TeamTechnician: $resource(ENV.NITRO_PROJECT_REST_URL + '/teamtechnicians/:teamtechnicianId', {}, {
add: {
  url: ENV.NITRO_PROJECT_REST_URL + '/teamtechnicians/:teamId/:technicianId',
  method: 'POST'
}

但它是行不通的。参数未在通过

But it would not work. The parameters were not passed in.

试了几次,我发现后添加一些参数定义时,自定义操作定义之前正确的,它的工作。

After a few tries I found out it worked when adding some parameter definition, right before the custom action definition.

它必须像:

TeamTechnician: $resource(ENV.NITRO_PROJECT_REST_URL + '/teamtechnicians/:teamtechnicianId', {
  teamId: '@teamId',
  technicianId: '@technicianId'
}, {
add: {
  url: ENV.NITRO_PROJECT_REST_URL + '/teamtechnicians/:teamId/:technicianId',
  method: 'POST'
}

请注意的presence:

Note the presence of:

teamId: '@teamId',
technicianId: '@technicianId'

我的理解是,然后,在$资源定义,即有多个路径参数自定义操作,要求他们用@符号来定义。

My understanding was then that in a $resource definition, a custom action that has more than one path parameter, requires them to be defined with @ signs.

而当它不只有一个。

这是为什么?

和为什么不能路径参数自定义操作,而不是在上面的资源被宣布?

And why can't the path parameters be declared in the custom action instead of above in the resource ?

推荐答案

参数可以每个自定义操作中声明。结果
默认参数是什么顾名思义:默认参数。(如:在其他参数不提供情况下使用)

The parameters can be declared per custom action.
The default parameters are what their name implies: default parameters (as in: "used in case other parameters are not provided").

使用'@'(在默认参数或在行动参数)的不可以强制性的。结果
它提供便利,并有特殊的意义。 paramKey:@someProp表示:结果
对于有一个请求主体的方法(例如POST,PUT等),如果我没有明确规定参数 paramKey 的值,请看我的数据对象的命名属性 someProp 并使用它的值作为 paramKey 参数的值。

The use of '@' (either in default parameters or in action parameters) is not mandatory.
It is provided as a convenience and has a special meaning. paramKey: '@someProp' means:
"For methods that have a request body (e.g. POST, PUT etc), if I do not explicitly provide a value for the parameter paramKey, please look into my data object for a property named someProp and use its value as the value for the paramKey parameter."

请注意,当你使用一个类的方法,你必须明确提供的数据对象:

Note that when you use a class method, you have to explicitly provide the data object:

SomeResourceClass.save({.../* data object */...});

当您使用实例方法,该实例本身充当数据对象:

When you use an instance method, the instance itself acts as the data object:

var instance = SomeResourceClass.get(...);
instance.$save(); /* `instance` will act as the data object. */


又见这个 短演示


See, also, this short demo.

更新:

好像你要调用下面​​的自定义操作:

It seems like you want to call the following custom action:

add: {
    url: ENV.NITRO_PROJECT_REST_URL + '/teamtechnicians/:teamId/:technicianId',
    method: 'POST'
}

试图这样调用< ResourceClass>。新增({teamId:teamId,technicianId:technicianId})不能按预期工作,因为这间preT的(意定)params对象作为一个数据对象。

Trying to call it like this <ResourceClass>.add({teamId: teamId, technicianId: technicianId}) does not work as expected, as it interpret the (intended to be) params object as a data object.

ngResource 文件,对非GET方法(像你这样)的方法签名是:

From the ngResource documentation, the method signatures for "non-GET" methods (like yours) are:


  • 非GET下课的行动:Resource.action([参数],POSTDATA,[成功],[错误])

  • 非获得实例动作:例如$行动([参数],[成功],[错误])

从上面的exerpt,很显然,如果你只传递一个对象下课的行动号召,那么它是PTED为数据间$ P $ 对象(请求的有效载荷)。此外,如果您有 @ - prefixed默认参数,那么URL参数会得到针对数据对象(这就是为什么它使用默认参数的工作)。

From the above exerpt, it is clear that if you only pass one object in "class" action call, then it is interpreted as the data object (the request's payload). Additionally, if you have @-prefixed default parameters, then the URL parameters are going to get resolved against that data object (which is why it worked with default parameters).

为了对角国米preT的 PARAMS 对象作为 PARAMS (而不是数据对象),因为数据参数是强制性的,你需要调用它是这样的:

In order for Angular to interpret the params object as a params (and not data object) and since the data param is mandatory, you need to call it like this:

<ResourceClass>.add({teamId: teamId, technicianId: technicianId}, {})

(或者,您也可以使用 TeamTechnician 实例的,但那是另一回事。)

(Alternatively, you could be using a TeamTechnician instance, but that's another story.)

这篇关于如何使用AngularJS $资源的自定义操作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆