从Ember 2.0组件调用控制器操作

时间:2015-09-23 13:35:21

标签: javascript ember.js

现在Ember 2.0决定彻底删除Ember.View我遇到了从视图到控制器传递操作的问题。

App.SomeView = Ember.Component.extend({
   didInsertElement : function(){
     var _this = this;
     window.addEventListener("message",
        function(event) {
            _this.get("controller").send("foobar", event.data);
        }, false);
  }
});

App.SomeController = Ember.Controller.extend({
   actions: {
      foobar: function(param) {
         console.log("Yey", param);
      }
   }
});

因为我现在需要使用Ember.Component而不是Ember.View。当然然后this.get(“controller”)。send方法不再起作用了。是否有某种解决方法?

1 个答案:

答案 0 :(得分:8)

您可以在组件中使用sendAction()并在模板中为其分配处理程序。

// some-component.js 
this.sendAction('actionName', params);

// template
{{some-component actionName="foobar"}}

// controller
actions: {
   foobar(params) {
     alert('action received');
   }
}

详细信息:http://guides.emberjs.com/v2.0.0/components/sending-actions-from-components-to-your-application/

相关问题