查看子路径时,Ember隐藏父模板

时间:2016-01-15 14:01:52

标签: javascript ember.js handlebars.js

我正在为Ember的网站制作一个简单的博客。

我的路线:

Router.map(function() {
  this.route('home', { path: '/' });
  this.route('blog', function() {
    this.route('post', {path: '/:post_id'});
  });
});

我希望如此,当我点击/blog中的帖子并结束/blog/:post_id时,我会隐藏blog.hbs文件的内容并仅显示blog/post.hbs内容

我尝试在我的post.js路由文件中明确指定渲染模板,但事情保持同样的方式。

export default Ember.Route.extend({
  model: function(params) {
    return this.store.findRecord('post', params.post_id, { reload: true });
  },
  renderTemplate: function() {
    this.render('blog/post');
  }
});

阅读这里的文档并没有提示我任何其他想法。

https://guides.emberjs.com/v1.13.0/routing/rendering-a-template/

我从根本上使用Ember错了吗?如果我想隐藏父模板内容,我的帖子网址不应该是博客的子路径吗?

2 个答案:

答案 0 :(得分:5)

对不起,@ remi回答了这个工作,但根据惯例这不正确。

当你创建这样的东西时:

Router.map(function() {
  this.route('blog', function() {
    this.route('post', {path: '/:post_id'});
  });
});

隐式创建

Router.map(function() {
  this.route('blog', function() {
    this.route('index', { path: '/' }); // <------ this one
    this.route('post', {path: '/:post_id'});
  });
});

因此,如果您有嵌套路由器,但不想嵌套模板,请将博客模板(和路由)重命名为索引。这将是重命名:

与POD

app / blog / route.js - &gt;应用程序/博客/索引/ route.js

app / blog / template.hbs - &gt;应用程序/博客/索引/ template.hbs

没有POD

app / routes / blog.js - &gt;应用程序/路由/博客/ index.js

app / templates / blog.hbs - &gt;应用程序/模板/博客/ index.hbs

这在Official guide上有记录(链接是针对最新版本的,但它也适用于旧版本)

答案 1 :(得分:4)

我不知道,如果您不希望嵌套内容,为什么要嵌套路由。反正...

如果您只想让网址反映您的嵌套,请尝试以下操作:

Router.map(function() {
  this.route('home', { path: '/' });
  this.route('blog', { path: 'blogs' });
  this.route('post', { path: 'blog/:post_id'});
});