Ember路由器命名约定

Mar*_*sen 6 ember.js

我需要在ember中深度嵌套一些路径,我有类似的东西.

  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');
      });
    });
  });
Run Code Online (Sandbox Code Playgroud)

我期待的是这样的结构:

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
Run Code Online (Sandbox Code Playgroud)

但我得到了这个:

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
Run Code Online (Sandbox Code Playgroud)

我没有得到的是什么时候ember停止使用大写(WizardsGoogle)并开始使用命名空间(WizardsGoogle.Register).看似不一致让我感到困惑.我原以为他们中的任何一个.

dar*_*123 3

我在深度嵌套资源中遇到了同样的事情。虽然我不知道这是怎么发生的,但我可以告诉你的是,你总是可以使用没有命名空间的 CapitalizedNestedRoute,并且 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');
  }
});
Run Code Online (Sandbox Code Playgroud)

在此示例中,应用程序将毫无问题地转换到 WizardsGoogleRegisterStep1Route。如果您使用容器来查找这样的路线:

App.__container__.lookup('route:wizards.google.register.step1').constructor
Run Code Online (Sandbox Code Playgroud)

它还将显示App.WizardsGoogleRegisterStep1Route. 和 Ember Guide 描述的一样。http://emberjs.com/guides/routing/defining-your-routes/#toc_nested-resources 并且 Ember Guide 没有引入命名空间路由。

所以我认为最好按照 Ember Guide 的建议(始终使用 CapitalizedNestedRoute)。CapitalizedNestedRoute在我看来,它比 更容易定义nested.namespace.route

最后,如果你真的想使用命名空间route/controller/template,你可以看看Ember.DefaultResolver。检查 API 以了解如何扩展它,以便容器可以按照您自己的规则查找模块。