Send() undefined for nested routes


#1

I’m trying access the action from the parent controller in the child controller. The following error occurs : Uncaught TypeError: Cannot read property ‘send’ of undefined. How to use the parent controller’s action in child controller ?

Parent Hbs :

Parent Controller : Child Hbs : Child controller:


#2

Hey, Best idea would be to extract message action to mixin and extend both controllers with it.


#3

Thanks for your reply! But I’m pretty new to ember and I have not used mixins here.


#4

No problem, you can learn about mixins here https://www.emberjs.com/api/ember/release/classes/Mixin


#5

So I believe the needs syntax is no longer a thing, and if it was you’d want the “needs” at the root level of the controller anyway, not in the actions hash. If you want to do something like this in modern Ember I believe you can do the following:

import Controller, {
  inject as controller
} from '@ember/controller';

export default Controller.extend({
  parent: controller(),

  actions: {
    message() {
      this.get('parent').send('message');
    }
  }
});

#6

@dknutsen Okay, got it. Thank you.