0
votes

I am trying to define a custom action to my Ember component & want to communicate between parent/child.

In my-child.js, I have

actions: {
    someCustomAction: function() {
        let self = this;
        self.sendAction('someCustomAction');
    },
}

And I catch the same in my-parent.js as below;

actions: {
    someCustomAction: function (){
        console.log("Inside someCustomAction...."); 
    }       
}

Now, with the above code, control/action does not come to my-parent.js

I have to add "someCustomAction" to the template as below

In my-parent.hbs

{{my-child someCustomAction="someCustomAction"}}

I wanted to know the exact reason for the same. Why does just doing sendAction not automtically work ?

2

2 Answers

1
votes

Here's how I would go about it while following the data down, actions up (DDAU) pattern.

First, you pass the action you want to fire in your child using the action helper.

{{my-child someActionName=(action 'parentAction')}}

Then, you retreive the action and fire it from the child component.

this.get('someActionName')(params);

Ember Twiddle Complete Example

1
votes

This is because components are not contextually aware. So the actions need to be tied together manually. But this also gives you the ability to send different actions on the same component and even additional args as well.

Components are isolated from their surroundings, so any data that the component needs has to be passed in. https://guides.emberjs.com/v2.15.0/components/passing-properties-to-a-component/

If the component is truly isolated, actions are as well. https://guides.emberjs.com/v2.15.0/components/triggering-changes-with-actions/#toc_passing-the-action-to-the-component