将参数传递给Ember triggerAction,它调用带参数的动作

 梦蕾AngeL 发布于 2023-02-13 12:33

在我的EmberApp中,我有一个视图,在完成一个动作后,调用另一个"视图"动作.这是一个childView,我基本上这样做:


在父视图中,

Ember.View.extend({

   actions:{
       doSomething: function(){
            //perform tasks related to this view
            this.get('childViewName').triggerAction({action:"someAction", target:this.get('childViewName')}) 
       // invoke action that belongs to a child view
      }

});

在http://emberjs.com/api/classes/Ember.ViewTargetActionSupport.html#method_triggerAction中指定的子视图传入Ember.TargetActionSupport mixin,并在其自己的操作中具有以下内容:

Ember.ChildView.extend(Ember.ViewTargetActionSupport,{
   actions:{
       someAction:function(){
            console.log("some action called from the Parent view");  // executes fine
       }
     }
 });
});

如您所知,这段代码按照应有的方式执行.但是,' someAction ' 实际上接受参数(模型).通过提供' this '关键字作为参数,可以非常轻松地将此模型提供给我的Handlebars按钮表达式.

 

它也可以在'doSomething'acton中通过简单地声明doSomething接受参数来检索,如下所示:

doSomething(modelInstance){
// modelInstance parameter will map to this keyword as specified in the handlebars action 
}

问题是,我不知道如何通过triggerAction调用将此modelInstance或'this'关键字发送到我的ChildView操作.TriggerAction只接受'action'关键字和docs中提到的target参数.

任何想法/替代解决方案?

1 个回答
  • actionContext将设置动作的this,因此您可以使用它设置动作的this

    doSomething: function(model){
       //perform tasks related to this view
       this.get('childViewName').triggerAction({action:"someAction", target:this.get('childViewName'), actionContext:model}) 
           // invoke action that belongs to a child view
    }
    
    
    someAction:function(){
       console.log(this);  // this would be the model from doSomething
    }
    

    2023-02-13 12:35 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有