簡體   English   中英

在Ember路線中,如何檢查是否存在動作?

[英]In an Ember route how can I check if an action exists?

在組件中,為組件提供可選操作非常容易。 在組件的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)也不起作用。

除了保留硬編碼的動作列表之外,我該如何實現這一目標?

如果你將你的行動保留在routes / application.js文件本身,那么代碼就是

在Ember 2.0或更高版本中:

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

在Ember 1.13

在Ember 1.13中,this.actions未定義,你必須使用this._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的答案非常有用。

您可以檢查controller.actions的操作。 在您的情況下,您必須檢查為

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

這是一個演示

如果你在一個組件中,你可以使用

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM