Ember.js 余烬路由器命名约定

Ember.js 余烬路由器命名约定,ember.js,Ember.js,我有一个深嵌套在余烬的一些路线的需要,我有这样的东西 this.resource('wizards', { path: '/wizards' }, function() { this.resource('wizards.google', { path: '/google' }, function() { this.resource('wizards.google.register', { path: '/register'

我有一个深嵌套在余烬的一些路线的需要,我有这样的东西

  this.resource('wizards', {
    path: '/wizards'
  }, function() {
    this.resource('wizards.google', {
      path: '/google'
    }, function() {
      this.resource('wizards.google.register', {
        path: '/register'
      }, function() {
          this.route('step1');
          this.route('step2');
          this.route('step3');
          this.route('summary');
      });
    });
  });
我所期待的是这样的结构:

url        /wizards/google/register/step1
route name wizards.google.register.step1
route      Wizards.Google.Register.Step1Route
Controller Wizards.Google.Register.Step1Controller
template   wizards/google/register/step1
App.__container__.lookup('route:wizards.google.register.step1').constructor
但我明白了:

url        /wizards/google/register/step1 //as expected
route name wizards.google.register.step1 //as expected
route      WizardsGoogle.Register.Step1Route
Controller WizardsGoogle.Register.Step1Controller
template   wizards/google.register.step1
我不知道ember什么时候停止使用大写(WizardsGoogle)并开始使用名称空间(WizardsGoogle.Register)。表面上的矛盾使我困惑。我可能会想到它们中的任何一个。

路由在资源中是“名称空间”的。资源使用你称之为资本化的东西,它们在某种程度上定义了一个名称空间(供路由使用)

所以这组路线:

App.Router.map(function() {
  this.resource('posts', function() {
    this.route('new');
    this.route('old');
    this.route('edit');
    this.route('whatever');
  });
});
App.Router.map(function() {
  this.resource('posts', function() {
    this.resource('photos');
    this.resource('comments');
    this.resource('likes');
    this.resource('teets');
  });
});
将产生具有以下名称的路由:

PostsRoute
PostsNewRoute
PostsOldRoute
PostsEditRoute
PostsWhateverRoute
鉴于,以下一组路线:

App.Router.map(function() {
  this.resource('posts', function() {
    this.route('new');
    this.route('old');
    this.route('edit');
    this.route('whatever');
  });
});
App.Router.map(function() {
  this.resource('posts', function() {
    this.resource('photos');
    this.resource('comments');
    this.resource('likes');
    this.resource('teets');
  });
});
将产生具有以下名称的路由:

PostsRoute
PhotosRoute
CommentsRoute
LikesRoute
TeetsRoute
还要注意的是,资源中的资源与“父”资源之间没有“名称空间”,因此您将始终拥有以下表单:

{CapitalizedResourceName}Route // for resources
{CapitalizedParentResourceName}{RouteName}Route // for routes

我希望这对你有帮助

我在深层嵌套资源中遇到了同样的问题。虽然我不知道这是怎么发生的,但我能告诉你的是,你可以在没有名称空间的情况下使用CapitaledEndestudoote,Ember可以识别它。尽管在Ember Inspector中显示“WizardsGoogle.Register.Step1Route”

在您的示例中,我定义了这样的路线:

App = Em.Application.create();

App.Router.map(function() {
  this.resource('wizards', function() {
    this.resource('wizards.google', function() {
      this.resource('wizards.google.register', function() {
        this.route('step1');
        this.route('step2');
        this.route('step3');
      });
    });
  });
});

App.IndexRoute = Em.Route.extend({
  beforeModel: function() {
    // Transition to step1 route
    this.transitionTo('wizards.google.register.step1');
  }
});

App.WizardsGoogleRegisterStep1Route = Em.Route.extend({
  model: function() {
    // You can see this alert when you enter index page.
    alert('a');
  }
});
在本例中,应用程序将毫无问题地过渡到WizardsGoogleRegisterStep1Route。如果您使用容器查找这样的路线:

url        /wizards/google/register/step1
route name wizards.google.register.step1
route      Wizards.Google.Register.Step1Route
Controller Wizards.Google.Register.Step1Controller
template   wizards/google/register/step1
App.__container__.lookup('route:wizards.google.register.step1').constructor
它还将显示
App.WizardsGoogleRegisterStep1Route
。这和《灰烬指南》描述的一样。《灰烬指南》并没有引入名称空间路由

因此,我认为最好按照《灰烬指南》的建议(始终使用大写的Estedroote)。在我看来,定义
capitalednestedLote
nested.namespace.route
更容易


最后,如果您真的想使用名称空间route/controller/template,可以查看。查看API以了解如何扩展它,以便容器可以根据自己的规则查找模块。

我理解这一点,因为这是非常简单的示例。Ember每次为资源下的路由“命名空间”时都使用大写字符。我还理解,像您所说的嵌套资源是“全球化的”,因此不是
PostsPhotos
。但是为什么
wizards.google.register
会变成
WizardsGoogle.register
而不是
WizardsGoogleRegister
wizards.google.register
。发现这是一个非常有趣的问题,特别是因为余烬文档似乎建议它应该完全按照您的预期工作。我所能找到的(通过搜索/测试)是,它显然只工作1级深。。。虽然没有逻辑上的理由说明为什么会这样。希望有人能给你一个好的答案。谢谢,这能澄清更多。看看扩展解析器是否值得,或者我是否会坚持使用超长名称。:)他们仍然对这种方法感到惊讶。