铁路由器/流星-在URL中发布带有用户名的详细信息

问题描述:

我对Meteor(尤其是Iron Router)比较陌生,并且一直困扰于以下问题...

I'm relatively new to Meteor (especially Iron Router), and have been stuck on the following issue...

我有一条路线可以显示有关单个帖子的详细信息:

I have a route which displays details about a single post:

    this.route('singlePost',{
      path:'/posts/:_id',
      data:function(){
        return Posts.findOne(this.params._id);
      }
    });

这很好,但是我希望能够在URL中显示帖子所有者的用户名,而不是静态的"/posts/"路径,例如:

This works fine, but I'd like to be able to show the post owner's username in the URL, rather than the static "/posts/" path, ex:

    this.route('singlePost',{
      path:'/:username/:_id',
      data:function(){
        return Posts.findOne(this.params._id);
      }
    });

post对象包含所有者的用户ID,但不包含用户名(用户名位于Meteor.users集合中).

The post object includes the user Id of the owner, but not the username (username is in the Meteor.users collection).

当我尝试使用2个动态值(用户名,发布ID)设置路由时,pathFor链接消失了(我认为是因为它在返回的发布对象中找不到用户名").

When I try to set the route with 2 dynamic values (username, post Id), the pathFor link disappears (I assume because it cannot find "username" in the post object that is returned).

如何获得识别用户名的途径?我假设对用户集合有一些查找功能,但是我不确定何时/何地.另外,我如何能够验证路线以确保帖子归正确的用户名所有?

How can I get the route to recognize the username? I assume some lookup function to the Users collection but I'm not sure when/where. Also, how would I be able to validate the route to make sure the post is owned by the correct username?

编辑-这是代码:

router.js

    Router.configure({
      layoutTemplate: 'layout',
      loadingTemplate: 'loading',
      waitOn:function(){
        return Meteor.subscribe('posts') && Meteor.subscribe('users');
      }
    });

    Router.map(function() {
      this.route('home', {
        path: '/',
        data:function(){
          Session.set('pageView','list');
          return Posts.find();
        }
      });
      this.route('singlePost',{
        path:'/:username/:_id',
        data:function(){
          Session.set('pageView','single');
          return Posts.findOne(this.params._id);
        }
      });
    });

    Router.onBeforeAction('loading');

home.html

    <template name="home">
      {{> postsList}}
    </template>

posts_list.html

    <template name="postsList">
      <ul>
        {{#each posts}}
          {{> postBlock}}
        {{/each}}
      </ul>
    </template>

single_post.html

    <template name="singlePost">
      {{> postBlock}}
    </template>

post_block.html

    <template name="postBlock">

      {{#if pageView "list"}}
        <li>
          <a href="{{pathFor 'singlePost'}}">{{title}}</a><br/>
          Author: {{username}}
        </li>
      {{/if}}

      {{#if pageView "single"}}
        <h1>{{title}}</h1>
        <p>{{description}}</p>
        <p>Author: {{username}}</p>
      {{/if}}
    </template>

post_block.js

    Template.postBlock.helpers({
      username:function(){
        var user = getUserInfo(this.owner);
        return user.username;
      },
      pageView:function(type){
        return Session.get('pageView') == type;
      }
    });

functions.js

    getUserInfo = function(id){
      return Meteor.users.findOne(id);
    }

用户名在列表和详细信息视图上均正确输出,但是我无法获得包含该用户名的pathFor链接.

The username outputs correctly on both the list and the details views, however I cannot get the pathFor link to include the username.

在查看模板时,您似乎没有在{{pathFor 'singlePost'}}中传递用户名或ID.

Looking at your template, you appear to be not passing username or id in {{pathFor 'singlePost'}}.

应为{{pathFor 'singlePost' username=username _id=yourId}}

您的路线将可以正常工作.

Your route should work then.