Ember如何让孩子的路线进入他的父母?

时间:2015-01-12 20:18:15

标签: ember.js model routes

我是Ember的新手并尝试将内容呈现到category模板中。因此,如果我点击该类别,它将在类别模板中显示详细信息和列表内容。我测试了一些东西,但它没有用。我已经搜索过这个问题,但我无法解决。我希望你能帮助我。

最好的问候

app.js

TableNotices.Router.map(function() {
  this.resource('tableNotices', { path: '/' }, function(){
      this.resource('category', {path: ':id'}, function(){
          this.route('contents');
          this.resource('content', {path: '/content/:id'});
      });
  });
});

TableNotices.ContentsRoute = Ember.Route.extend({
    model: function() {
        return  this.modelFor('category').get('contents');
    }
});

TableNotices.Content = DS.Model.extend({
  content: DS.attr('string'),
  contentType: DS.attr('string'),
  orderPos: DS.attr('number'),
  category: DS.belongsTo('category')
});

TableNotices.Category = DS.Model.extend({
  name: DS.attr('string'),
  parent: DS.attr('number'),
  picture: DS.attr('string'),
  contents: DS.hasMany('content', {async:true})
});

的index.html:

   <script type="text/x-handlebars" data-template-name="category">
       {{name}}  
       {{outlet}}
    </script>
    <script type="text/x-handlebars" data-template-name="contents">
       foobar
    </script>

jsbin

2 个答案:

答案 0 :(得分:0)

在路线中,您可以使用renderTemplate指定render the templates的方式。

TableNotices.ContentsRoute = Ember.Route.extend({
  model: function() {
    return  this.modelFor('category').get('contents');
  }
  renderTemplate: function() {
    this.render('contents', {   // the template to render
      into: 'category',                // the template to render into
      outlet: 'category',              // the name of the outlet in that template
      controller: 'contents'        // the controller to use for the template
   });
 }
});

<script type="text/x-handlebars" data-template-name="category">
   {{name}}  
   {{outlet "contents"}}
</script>

答案 1 :(得分:0)

尽管Aaron Renoir的回答是有效的,但考虑到这一点的最佳方法是,因为您的路线是嵌套的,所以您的模板也需要嵌套。当激活这些嵌套路由时,Ember会将每个嵌套模板渲染到父模板中。这为您提供了一种将页面的不同部分委派给各种模板的好方法,这有助于保持内容的有序化。

每个“父”模板都需要{{outlet}}才能呈现子模板。