在我的 EmberApp 中,我有一个 View ,其中完成一个操作后,会调用另一个“ View ”操作。这是一个 childView,我基本上是这样做的:
<button {{action "doSomething" target="view"}}>DO something</button>
在父 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
}
});
subview ,如 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 按钮表达式。/strong>' 关键字作为参数。
<button {{action "doSomething" this target="view"}}>DO something</button>
也可以通过简单地声明 doSomething 接受一个参数来在“doSomething”acton 中检索它,如下所示:
doSomething(modelInstance){
// modelInstance parameter will map to this keyword as specified in the handlebars action
}
问题是,我不知道如何通过triggerAction 调用将此 modelInstance 或“this”关键字发送到我的 ChildView 操作。 TriggerAction 仅接受文档中提到的“action”关键字和目标参数。
有什么想法/替代解决方案吗?
最佳答案
actionContext会设置action的this,因此你可以使用它来设置action的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
}
关于ember.js - 将参数传递给 Ember triggerAction,后者调用带有参数的操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20340963/