1

我有一条使用流路由器postDetail的路径。/posts/:postId

我想检查帖子是否真的存在。如果帖子不存在,我想显示postList路线。

我怎样才能做到这一点?我想我可以使用triggersEnter;但是,数据是在模板中订阅的,所以也许我不能triggersEnter在路由器中使用。

一种简单的方法是doesExists使用模板助手中的变量填充模板,然后使用

{{#if doesExists}}
  [...]
{{else}}
  {{> postList}}
{{/if}}

但我认为这不是一个非常聪明的方法,因为我必须在很多不同的模板中这样做,而且我无法使用这种方法将用户重定向到 postList 路由。

4

1 回答 1

0

如果您有模板级别的订阅,例如:

Template.YOUR_TEMPLATE_HERE.onCreated(function() {
  let self = this;
  self.autorun(function() {
    self.subscribe('posts');
  })
});

然后您可以使用 triggersEnter 重定向,如下所示:

triggersEnter: [function(context, redirect) {
  if (Posts.find({_id:context.params.id}).count() < 1)
    redirect('/postslist');
}],
于 2016-01-13T15:23:17.083 回答