简体   繁体   中英

Calling controller action from Ember 2.0 component

Now that Ember 2.0 decided to remove the Ember.View completely I am having issues of passing actions from the view to the controller.

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);
      }
   }
});

Because instead of Ember.View I need to use now Ember.Component. And of course then this.get("controller").send method does not work anymore. Is there some kind of workaround for this?

You could use sendAction() in component and assign handler to it in template.

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

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

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

Details: http://guides.emberjs.com/v2.0.0/components/sending-actions-from-components-to-your-application/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM