ember.js - 将参数传递给 Ember triggerAction,后者调用带有参数的操作

标签 ember.js handlebars.js

在我的 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/

相关文章:

javascript - 在 Meteor.JS Handlebars 中将对象转换为数组后无法拆分字符串

ember.js - 如何从 block 形式组件 Ember.js 中插入的模板触发组件的操作?

javascript - 将多个 native 事件映射到 Ember.js 中的语义应用程序事件?

ember.js - Ember-CLI:找不到模块 'broccoli-static-compiler' 错误:找不到模块 'broccoli-static-compiler'

javascript - Ember.js 中带有 {{render}} 帮助器的嵌套 Controller 的 Controller 绑定(bind)

javascript - 我希望我的操作能够更改它所附加的 DOM 元素

javascript - 使用 javascript 格式化货币的 Handlebars 功能

javascript - 每次迭代在 Handlebars 内选择带有 jquery 的元素

javascript - jQuery Mobile 和 Emberjs - 选择小部件

javascript - 填充隐藏表单字段