1

在我的用例中,我想从父路由导航到子路由以及从子路由导航到父路由。第一个案例有效,但第二个无效。为什么?怎么了?是renderTemplate中的错误吗?

App = Ember.Application.create();

App.Router.map(function() {

  this.resource('parent',function(){
      this.resource('child',function(){
    });
  });

});

App.ChildRoute = Ember.Route.extend({

  renderTemplate: function(){
    this.render('child', {
      into: 'application',
      controller: 'child'
    });
  }
});


<script type="text/x-handlebars">
  <h1>Ember Sandbox</h1>

  <nav>
    {{#linkTo 'parent'}} Parent {{/linkTo}}
  </nav>

  {{outlet}}
</script>

<script type="text/x-handlebars" data-template-name="index">
  <h2>Welcome</h2>
</script>

<script type="text/x-handlebars" data-template-name="parent">
  <h2>Parent</h2>
  {{#linkTo 'child'}} Child {{/linkTo}}

</script>

<script type="text/x-handlebars" data-template-name="child">
  <h2>Child</h2>
  {{#linkTo 'parent'}} Parent {{/linkTo}}
</script>
4

1 回答 1

3

每当您嵌套资源时,emberjs 都会提供一个索引路由(App.ParentIndexRoute)。当您从子资源转换到父资源时,父模板已经呈现,因此它将被重定向到索引路由(App.ParentIndexRoute)。

在 App.ParentIndexRoute 中呈现您的父模板将解决您的问题

App.ParentIndexRoute = Ember.Route.extend({
 renderTemplate: function(){
  this.render('parent', {into: 'application'});
 }
});

你的工作 jsbin

于 2013-07-25T07:39:21.043 回答