【问题标题】:Getting access to Ember ApplicationController from another view从另一个视图访问 Ember ApplicationController
【发布时间】:2013-03-27 15:14:57
【问题描述】:

我想在 ApplicationController 中初始化所有用户的列表,然后在另一个视图的下拉列表中显示它们。如何从不同的视图访问 ApplicationController?

相关代码如下:

  App.ApplicationRoute = Ember.Route.extend({
   setupController:function(controller) {
    controller.set('users', App.User.find());
    controller.set('selectedUser', null);
  }
 });

 <script type="text/x-handlebars" data-template-name="users">
   {{view Ember.Select
   contentBinding="App.ApplicationController.users"
   optionValuePath="content.id"
   optionLabelPath="content.fullName"
   selectionBinding="App.ApplicationControllerselectedUser"}}

   selected user: {{App.ApplicationController.selectedUser.fullName}}
 </script>

【问题讨论】:

    标签: ember.js


    【解决方案1】:

    在视图的控制器中指定needs

    App.UsersController = Ember.Controller.extend({
      needs: ['application']
    });
    

    在您看来,您可以按如下方式访问应用程序控制器

    controllers.application
    

    在你的例子中

    <script type="text/x-handlebars" data-template-name="users">
      {{view Ember.Select
             contentBinding="controllers.application.users"
             optionValuePath="content.id"
             optionLabelPath="content.fullName"
             selectionBinding="controllers.application.selectedUser"}}
    
      selected user: {{controllers.application.selectedUser.fullName}}
    </script>
    

    【讨论】:

    • 疯了,当我使用{{debugger}} 时没有出现,即this.controllers 返回null。但是现在可以了!
    • 这是因为模板上下文中的this 等价于controller。这可以通过打印构造函数来证明,例如:{{this.constructor}} // App.ApplicationController {{controller.constructor}} // App.ApplicationController:ember
    【解决方案2】:

    您可以通过容器查找控制器

    this.container.lookup('controller:application')
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-08-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多