将参数传递给Ember triggerAction,它调用接受参数的操作 [英] Passing parameters to Ember triggerAction which calls an action that takes parameters

查看:144
本文介绍了将参数传递给Ember triggerAction,它调用接受参数的操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

 < button {{actiondoSomethingtarget =view }}> DO something< / button> 

在父视图中,

  Ember.View.extend({

actions:{
doSomething:function(){
//执行与此视图相关的任务
this.get('childViewName')。triggerAction({action:someAction,target:this.get('childViewName')})
//调用属于子视图的操作
}

});



孩子视图,如 http://emberjs.com/api/classes/Ember中所述。 ViewTargetActionSupport.html#method_triggerAction ,在Ember.TargetActionSupport mixin中传递,并在其自己的操作中具有以下内容:

  Ember.ChildView.extend(Ember.ViewTargetActionSupport,{
actions:{
someAction:function(){
console.log(从父视图调用的一些操作); //执行罚款
}
}
});
});

正如你所知,这段代码应该执行。但是, someAction 实际上带有一个参数(一个模型)。可以很容易地通过提供这个'关键字作为参数。

 < button {{actiondoSomethingthis target =view}} < / button> 

它也可以在'doSomething'acton中检索,只需说明doSomething接受一个参数,如这样:

  doSomething(modelInstance){
// modelInstance参数将映射到句柄操作中指定的此关键字
}

问题是,我不知道如何发送这个modelInstance或'this'关键字通过triggerAction调用到我的ChildView动作。 TriggerAction只接受文档中提到的'action'关键字和target参数。



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

解决方案

actionContext将设置这个动作,所以你可以使用它来设置这个动作

  doSomething:function(model){
//执行与此视图相关的任务
this.get('childViewName')。triggerAction({action:someAction,target:this.get('childViewName'),actionContext:model})
//调用属于子视图的操作
}


someAction:function(){
console.log(this); //这将是从doSomething


的模型

In my EmberApp, I have a view, in which, upon the completion of an action, calls another 'view' action. This is a childView, and I essentially do this like this:

<button {{action "doSomething"  target="view"}}>DO something</button>

In the parent view,

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
      }

});

The child view, as specified in http://emberjs.com/api/classes/Ember.ViewTargetActionSupport.html#method_triggerAction , passes in an Ember.TargetActionSupport mixin, and in its own actions, has the following:

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

As you can tell, this piece of code executes as it ought to. However, 'someAction' actually takes in a parameter (a model). This model can be given to my Handlebars button expression quite easily by providing the 'this' keyword as a parameter.

 <button {{action "doSomething" this target="view"}}>DO something</button>

It can also be retrieved in 'doSomething' acton by simply stating that doSomething takes in a parameter, like this:

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

The problem is, I don't know how to send this modelInstance or the 'this' keyword to my ChildView action through the triggerAction call. TriggerAction only takes in the 'action' keyword and the target parameter as mentioned in the docs.

Any idea/alternate solutions ?

解决方案

actionContext will set the this of the action, so you can set the this of the action using it

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
}

这篇关于将参数传递给Ember triggerAction,它调用接受参数的操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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