Passing parameters to Ember triggerAction which calls an action that takes parameters

瘦欲@ 提交于 2019-12-10 20:22:07

问题


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 ?


回答1:


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
}


来源:https://stackoverflow.com/questions/20340963/passing-parameters-to-ember-triggeraction-which-calls-an-action-that-takes-param

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!