Ember.js 在余烬路径中,如何检查是否存在操作?

Ember.js 在余烬路径中,如何检查是否存在操作?,ember.js,Ember.js,在组件中,向组件提供可选操作非常容易。在组件的JS中,我可以写: if (this.get('someAction')) { this.sendAction('someAction'); } 在我的应用程序路径中,我有一个“通用操作”,它为我提供了一长串操作的小部件组件,看起来如下: genericAction: function(customActionName, customActionParams) { this.send(customActionName, customActi

在组件中,向组件提供可选操作非常容易。在组件的JS中,我可以写:

if (this.get('someAction')) {
  this.sendAction('someAction');
}
在我的应用程序路径中,我有一个“通用操作”,它为我提供了一长串操作的小部件组件,看起来如下:

genericAction: function(customActionName, customActionParams) {
  this.send(customActionName, customActionParams);
}
出于各种原因(包括在某些组件中使用genericAction来触发测试可以订阅的操作,但应用程序不一定在某些难以测试的异步/假装工作流中使用),我更愿意检查操作是否存在,即:

genericAction: function(customActionName, customActionParams) {
  if (this.get(customActionName)) {
    this.send(customActionName, customActionParams);
  }
}
与在组件中执行类似,但是这不起作用,
this.controller.get(customActionName)


除了保留一个硬编码的操作列表外,我如何实现这一点?

您可以在
controller.actions
中检查操作。在您的情况下,您必须检查为

   if(Em.get(this.controller.actions, actionName)) {
         this.get('controller').send(actionName);
   }

下面是一个

如果您将操作保存在routes/application.js文件中,那么代码将是

在Ember 2.0或更高版本中:

   if(Em.get(this.actions, actionName)) {
         this.send(actionName);
   }
在余烬1.13中

此.actions在Ember 1.13中未定义,您必须使用此.\u actions

   if(Em.get(this._actions, actionName)) {
         this.send(actionName);
   }
如果您需要同时支持Ember 1.x和2.x,请使用以下内容:

   let actions = this.actions || this._actions;
   if(Em.get(actions, actionName)) {
         this.send(actionName);
   }

如果您将操作保存在应用程序控制器(controllers/application.js)中,那么siva-abc的答案非常有用。

如果您位于可以使用的组件中

   if (this.get('yourActionName')) { }

我猜缺少一个括号:
if(Em.get(this.controller.actions,actionName)){this.get('controller').send(actionName);}