How to get an instance of a controller in Ember.js?
Asked Answered
C

4

6

I'm trying to access an instance of a controller that has been wired automatically using App.initialize();

I've tried the below but it returns a Class not an instance.

Ember.get('App.router.invitesController')

Collado answered 27/10, 2012 at 5:56 Comment(0)
L
9

This answer works with RC1/RC2.

Now you can use the needs declaration in order to make the desired controller accessible. Here's an example:

Suppose I want to get something from my SettingsController from within my ApplicationController. I can do the following:

App.SettingsController = Ember.Controller.extend({
  isPublic: true
});

App.ApplicationController = Ember.Controller.extend({
  needs: 'settings',
  isPublicBinding: 'controllers.settings.isPublic'
});

Now in the context of my ApplicationController, I can just do this.get('isPublic')

Labanna answered 3/4, 2013 at 2:7 Comment(1)
I got this to work using the latest EmberJS by using needs: ['settings'].Tonsil
G
19

I have a quick post about this exact subject on my Blog. It's a little big of a different method, but seems to work well for Ember.js RC1.

Check it out at: http://emersonlackey.com/article/emberjs-instance-of-controller-and-views

The basic idea is to do something like:

var myController = window.App.__container__.lookup('controller:Posts');
Golliner answered 14/3, 2013 at 4:6 Comment(2)
Yehuda has interesting comments about this. He actually changed container from just container because he didn't want people using this "private" api. I need to access a controller from my main App application and see no alternative to this. Actually, looking through discourse's source code, they are doing something like this also. Discourse.__container__.lookup('controller:modal').get('currentView'); (Discourse was created by Steve Atwood he created Stack Overflow, so it seems that it is the only alternative right now.California
It... wokrs, thanks you. But seriousely, we have nothing less crappy ?to do that ?Justifiable
L
9

This answer works with RC1/RC2.

Now you can use the needs declaration in order to make the desired controller accessible. Here's an example:

Suppose I want to get something from my SettingsController from within my ApplicationController. I can do the following:

App.SettingsController = Ember.Controller.extend({
  isPublic: true
});

App.ApplicationController = Ember.Controller.extend({
  needs: 'settings',
  isPublicBinding: 'controllers.settings.isPublic'
});

Now in the context of my ApplicationController, I can just do this.get('isPublic')

Labanna answered 3/4, 2013 at 2:7 Comment(1)
I got this to work using the latest EmberJS by using needs: ['settings'].Tonsil
B
3

You can access a controller instance inside an action in the router via router.get('invitesController'), see http://jsfiddle.net/pangratz666/Pk4k2/:

App.InvitesController = Ember.ArrayController.extend();

App.Router = Ember.Router.extend({
    root: Ember.Route.extend({
        route: '/',
        index: Ember.Route.extend({
            route: '/',
            connectOutlets: function(router, context) {
                var invitesController = router.get('invitesController');
            },
            anAction: function(router) {
                var invitesController = router.get('invitesController');
            }
        })
    })
});​
Bozuwa answered 27/10, 2012 at 6:24 Comment(0)
A
0

You can access any controller instance by name using lookup method of Application instance. To get Application instance you can use getOwner from any route or controller.

const controllerName = 'invites';
Ember.getOwner(this).lookup(`controller:${controllerName}`));

Works for me in Ember 2.4 - 3.4.

Adore answered 7/7, 2020 at 4:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.