我如何获得ApplicationController?

Bob*_*Bob 9 ember.js

如何在HomeController中获取ApplicationController?

 HomeController=Ember.Controller.Extend({
   init:function {
        // Here I want to get My application controller. Is there any way to get??
   }

 })
Run Code Online (Sandbox Code Playgroud)

Mik*_*tti 17

如何在HomeController中获取ApplicationController?

要指定控制器之间的依赖关系(或需求),请使用以下needs属性:

HomeController=Ember.Controller.Extend({
  needs: ['application']
})
Run Code Online (Sandbox Code Playgroud)

现在,Ember将使您的HomeController内部的应用程序控制器可以访问controllers.application.您可以像使用其他任何内容一样使用它HomeController,甚至可以从您的模板访问它:

<!-- inside `home` template -->
{{controllers.application}}
Run Code Online (Sandbox Code Playgroud)

有关控制器需求的更多详细信息,请参见http://darthdeus.github.com/blog/2013/01/27/controllers-needs-explained/.

顺便说一句:ember控制器很少使用自定义init fx.考虑移动任何初始化逻辑来setupController代替路由.

  • 2013年10月:我不得不使用`this.get('controllers.application')`而不是`controllers.application`. (3认同)