Tim Baas
Tim Baas

Reputation: 6185

In EmberJS built by EmberCLI, how to call methods on different controllers?

I'm building an app in Ember with EmberCLI.

In my top navigation (controllers/navigation/top.js) I have an action that is triggered on a button click. This action should open the left navigation (controllers/navigation/left.js).

My top navigation controller:

import Ember from "ember";

export default Ember.Controller.extend({
  actions: {
    toggleMenu: function() {
      // I need to call toggleProperty on the left navigation controller. 
      [leftNavigationController].toggleProperty('visible');
    }
  }
});

How to get another controllers instance to call a method like toggleProperty?

Upvotes: 1

Views: 163

Answers (1)

Kingpin2k
Kingpin2k

Reputation: 47367

in a controller you would use needs and then get the controller and call it

App.FooController = Ember.ObjectController.extend({
  needs:['bar'],
  blah: function(){
    var barController = this.get('controllers.bar');
    barController.toggleProperty('visible');
  }
});

http://emberjs.jsbin.com/dofedehi/2/edit

Upvotes: 3

Related Questions