Ember.js:两个控制器之间的依赖关系失败

ran*_*its 5 javascript ember.js

我试图访问needs在兄弟控制器上使用的控制器中的两个模型之一.我的路由器如下所示:

App.Router.map(function() {
    this.route('login');
    this.route('mlb.lineups', {path: 'tools/mlb/lineups'})
    this.resource('mlb.lineups.site', { path: 'tools/mlb/lineups/site/:site_id' });
});
Run Code Online (Sandbox Code Playgroud)

mlb.lineups路由定义如下所示:

App.MlbLineupsRoute = Ember.Route.extend({
    model: function() {
      var self = this;
      return Ember.RSVP.hash({
        sites: self.store.find('site')
      })
  },

  setupController: function(controller, models) {
    controller.set('model', models.get('sites'));
  },

  afterModel: function(models) {
    var site = models.sites.get('firstObject');
    this.transitionTo('mlb.lineups.site', site);
  }
});
Run Code Online (Sandbox Code Playgroud)

我在这里使用Ember.RSVP.hash({})的原因是我计划在检索site模型后添加另一个要检索的模型.

现在MlbLineupsSiteController我在尝试sites使用以下内容访问模型:

App.MlbLineupsSiteController = Ember.ArrayController.extend({
    needs: "mlb.lineups",
    sites: Ember.computed.alias("controllers.models.sites")
});
Run Code Online (Sandbox Code Playgroud)

这是我在Ember控制台中遇到的错误: needs must not specify dependencies with periods in their names (mlb.lineups)

sitesMlbLineups我的控制器中获取模型的最佳方法是MlbLineupsSiteController什么?

Dan*_*mak 10

注意:


@NicholasJohn16的答案已经无效了.它始终会出现无法找到控制器的错误.通常,您也应该永远不要使用needs属性,并且如果必须使控制器相互依赖,则始终使用Ember.inject.controller.我还建议使用服务而不是控制器之间的依赖关系.通过服务维护包含控制器之间通信的代码比直接访问其他控制器属性的控制器更容易.您可能并不总是意识到这种访问,使用服务可以为您提供另一层安全性.

解:


测试在Ember.js 1.10.0-beta.4.使用Controller中的以下代码引用嵌套控制器needs:

needs: ['classic/about']
Run Code Online (Sandbox Code Playgroud)

然后你可以使用以下方法访问它:

const aboutController = this.get('controllers.classic/about');
const aboutProperty   = aboutController.get('customProperty');
Run Code Online (Sandbox Code Playgroud)

按预期工作.基本上你需要用斜线替换.


Nic*_*n16 3

它应该是:

needs:" MlbLineupsSite "

基本上,您想要包含的控制器的名称,减去控制器一词。

您发布的其他所有内容都应该有效。