sajid sharlemin
sajid sharlemin

Reputation: 162

call another controller method from an action method in emberjs

I am trying to call a second controller method from an action method. I am able to call second controller method using

this.controller.get('controllers.secondController').method();

How can i call method of another controller from action method? There is work around i know of like call the controller of the current action method which in turn calls the second controller method.

this.callFirstControllerMethod();

and then use

this.controller.get('controllers.secondController').method();

But is there a way to call the second controller method directly?

Upvotes: 0

Views: 1043

Answers (1)

Asgaroth
Asgaroth

Reputation: 4334

Generally this kind of logic should not be on the controller, a controller should really only have actions that relate to the UI or the display of your model.

I would suggest to add an action in the route, that can access the controllers and invoke any methods you need.

// route.js
actions: {
  doSomething: function() {
    var controllerA = this.controllerFor('firstController');
    var controllerB = this.controllerFor('secondController');

    controllerA.doSomething();
    controllerB.doSomethingElse();
  }
}

Upvotes: 2

Related Questions