如何计算父路由模型的项目以在嵌套路由中使用它?

How to count items of a parent-route-model to use it in a nested route?

我有这样的结构

App.Router.map(function() {
  this.route("levels");
  this.resource("level", {path: 'levels/:level_id'}, function()  {
    this.route("play");
  });
});

现在,我需要使用嵌套模型中的项目数(级别记录)信息 "level/play"。 我的第一种方法是在我的播放控制器中插入如下内容:

App.LevelPlayController = Ember.ObjectController.extend({

    getLevelCount: function() {
        var allLevels = this.store.find("level");
        var levelCount = allLevels.get("length");
        return levelCount;
    }.property()
}

但这似乎行不通。也许是因为异步加载?

有人可以帮忙吗?

由于父路由的model已经加载完毕,直接在子路由中访问即可:

Ember.Route.extend({
  model: function() {
    return this.modelFor('level');
  }
});

然后在您的控制器中,您可以访问级别 model 属性。

访问商店更多的是路由而不是控制器。