Ember hide parent template when viewing sub-route

白昼怎懂夜的黑 提交于 2019-12-10 16:05:36

问题


I am making a simple blog for my website with Ember.

My routes:

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

I want it so when I click on a post in /blog and wind up at /blog/:post_id I hide the content of the blog.hbs file and only show the blog/post.hbs content.

I tried specifying the render template explicitly in my post.js route file, but things kept working in the same fashion.

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

Reading through the documentation here didn't cue me into any other ideas.

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

Am I fundamentally using Ember wrong? Should my post URL not be a sub-route of Blog if I want to hide a parent templates content?


回答1:


Sorry, @remi answer do the job, but it is not the correct according to the convention.

When you create something like this:

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

It implicit creates

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

So, if you got nested router, but don't want to nest templates, rename the blog template (and route) to index. It will be something like renaming:

with POD

app/blog/route.js -> app/blog/index/route.js

app/blog/template.hbs -> app/blog/index/template.hbs

No POD

app/routes/blog.js -> app/routes/blog/index.js

app/templates/blog.hbs -> app/templates/blog/index.hbs

This is documented on the Official guide (the link is for the latest version, but it is also applicable for older ones)




回答2:


I don't know, why you nest the route if you don't want the content to be nested. Anyways...

If you just want the url to reflect your nesting, try this:

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


来源:https://stackoverflow.com/questions/34812757/ember-hide-parent-template-when-viewing-sub-route

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!