Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 265
Maksim Luzik Avatar asked Jul 18 '26 13:07

Maksim Luzik


1 Answers

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/

like image 130
artych Avatar answered Jul 21 '26 02:07

artych



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!