【问题标题】:Nested Resources in ember-cli, messy app structureember-cli中的嵌套资源,凌乱的应用程序结构
【发布时间】:2014-11-14 12:45:00
【问题描述】:

作为 Ember.js 和 ember-cli 的新手,我无法理解使我的应用正常运行所需的东西。

我的应用程序的逻辑层次结构是这样的:

Projects
|___Project
    |___Details
    |___Team Members
    |___Budget
        |___Planned
        |___Actual

目前,这是我的 router.js:

  this.resource('projects');
  this.resource('project', { path: 'projects/:project_id' }, function() {
    this.route('details');
    this.route('team');
    this.route('milestones');
    this.resource('budget', function(){
      this.route('project-budget', { path: 'project'});
      this.route('resource-budget', {path: 'team'});
    });
  });

我遇到的问题是将文件放在哪里。直到 Budget 下的两个嵌套路由点为止,我的文件夹结构看起来就像我的层次结构,但是由于 Resource 重置了命名空间,现在要使其正常工作,我必须将我的 Budget 模板、路由和控制器拉回顶部级别(带有项目的东西),这看起来很混乱,并且在以后尝试维护这个东西时会让人头疼。

我做错了吗?

【问题讨论】:

  • 预算资源似乎不合适,您嵌套在project 中的资源都没有使用动态细分,为什么它们首先存在。
  • @PatsyIssa:因为它们都是属于一个项目的片段,所以项目/细节,项目/团队等。

标签: javascript ember.js ember-cli


【解决方案1】:

路由器定义在 Ember 中可能有点棘手。您在 router.js 中的资源/路由定义应该反映您的页面结构。例如,如果你的 'team' 模板应该嵌套在你的 'project' 模板中,那么 'team' 应该嵌套在 router.js 中的 'project' 中:

Router.map(function() {
    this.resource('project', function() {
         this.route('team');
    });
});

如果您在 router.js 中使用 this.route(),那么您的文件夹结构应该模仿 router.js 中的结构。使用上面的例子,因为我们使用 this.route() 来定义'team',你的文件夹结构是这样的:

app/routes/project.js
app/routes/project/team.js
app/templates/project.hbs
app/templates/project/team.hbs

但是,如果您选择在 router.js 中使用 this.resource(),那么您就是在告诉 Ember 您将要重置文件夹结构。所以如果你把 router.js 改成这样:

Router.map(function() {
    this.resource('project', function() {
         this.resource('team');
    });
});

...那么您的文件夹结构将是这样的:

app/routes/project.js
app/routes/team.js
app/templates/project.hbs
app/templates/team.hbs

回到你的具体问题,如果你觉得重置文件夹结构很麻烦,那么你可以在任何地方使用 this.route() 并放弃 this.resource(),因为可嵌套的 this.route() 登陆 Ember 1.7 :http://emberjs.com/blog/2014/08/23/ember-1-7-0-released.html

【讨论】:

  • 我曾尝试使用“路由”而不是“资源”(违反 ember 建议将资源用于名词),但后来它抱怨需要资源才能做某事,所以我有切换回来。最终,我确实必须重置我的文件夹结构才能使其正常工作,但现在预算内容位于顶层,团队和详细信息位于项目中。也许我需要开始使用Pod Structure,但我害怕真的把事情搞砸了。虽然我不应该害怕,但我想用树枝。
  • 嵌套 this.route() 应该可以正常工作。下面是一个例子:emberjs.jsbin.com/fobofe/4/edit?html,js 我在 jsbin 中使用的是 globals 而不是 Ember CLI,但原理是一样的。
  • 您使用的是什么版本的 Ember?可嵌套的 this.route() 是在 1.7 版本中引入的:emberjs.com/blog/2014/08/23/ember-1-7-0-released.html
  • 为了支持 IE8,我不得不降级到 1.6。
  • 最新版本的 Ember 仍支持 IE8。其实计划是在 Ember 2.0 中仍然支持它:github.com/emberjs/rfcs/pull/15#issuecomment-61613154
猜你喜欢
  • 1970-01-01
  • 2011-03-14
  • 1970-01-01
  • 1970-01-01
  • 2015-12-07
  • 2017-03-08
  • 1970-01-01
  • 1970-01-01
  • 2014-12-24
相关资源
最近更新 更多