6

我正在尝试访问已使用 App.initialize(); 自动连接的控制器实例;

我已经尝试过以下方法,但它返回的是一个类而不是一个实例。

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

4

4 回答 4

19

我在我的博客上有一篇关于这个确切主题的快速帖子。这是一种不同的方法,但似乎适用于 Ember.js RC1。

查看:http ://emersonlackey.com/article/emberjs-instance-of-controller-and-views

基本思想是做类似的事情:

var myController = window.App.__container__.lookup('controller:Posts');
于 2013-03-14T04:06:30.297 回答
9

这个答案适用于 RC1/RC2。

现在您可以使用needs声明来使所需的控制器可访问。这是一个例子:

假设我想从SettingsController我的ApplicationController. 我可以执行以下操作:

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

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

现在在 my 的上下文中ApplicationController,我可以做this.get('isPublic')

于 2013-04-03T02:07:26.457 回答
3

您可以通过访问路由器中的操作内的控制器实例router.get('invitesController'),请参阅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');
            }
        })
    })
});​
于 2012-10-27T06:24:43.770 回答
0

您可以使用Application instance 的查找方法按名称访问任何控制器实例。要获取 Application 实例,您可以从任何路由或控制器中使用getOwner 。

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

在 Ember 2.4 - 3.4 中为我工作。

于 2020-07-07T04:22:38.000 回答