如何在Ember中筑巢两条路线?

时间:2014-07-27 20:24:14

标签: javascript ember.js

如何在ember中正确引用路线中的路线?我只看到用resource完成嵌套。这可能只用两条路线吗?

我创建了一个名为info的路由,嵌套在about路由中。但是,当我尝试使用{{#linkTo "info"}}Info{{/linkTo}}{{#linkTo "about.info"}}Info{{/linkTo}}引用它时,我在控制台中收到错误info route not found

App.js:

App = Ember.Application.create();

App.Router.map(function() {
  this.route('about', function(){
    this.route('info');
  });
  this.route('contact');
});

HBS:

<script type="text/x-handlebars" data-template-name="about">
  <h2>About</h2>
  <p>A little live editor for Ember, written in Ember.</p>

  {{ outlet }} 

</script>

<script type="text/x-handlebars" data-template-name="about/info">
  <h2>More Info</h2>
  <p>A little live editor for Ember, written in Ember.</p>
</script>

在jsbin中复制的示例:http://jsbin.com/fogozave/1/edit?html,js,output

1 个答案:

答案 0 :(得分:2)

故事:

路线是树上的叶子。叶子不会从其他叶子长出来。资源是树上的分支。分支可以有其他分支,它们也可以有叶子。叶子虽然是路的尽头。

作为无上下文语法(类似这样):

S as application root/resource
Re as resource
Ro as route

S  -> Re | Ro | SS | ε
Re -> Re | Ro | ReRe | ε 
Ro -> ε

现实:

App.Router.map(function() {
  this.route('foo');
  this.resource('colors', {path:'/colors'}, function(){
    this.route('bar');
    this.resource('color', {path: '/:id'}, function(){
      this.route('baz');
    });
  });
});

http://jsbin.com/fogozave/2/edit

查看此页面的资源部分。 http://emberjs.com/guides/routing/defining-your-routes/