Iron Router / Meteor - Post details with username in URL

社会主义新天地 提交于 2020-01-17 01:21:13

问题


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);
      }
    });

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);
      }
    });

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

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?

Edit - here is the code:

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);
    }

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


回答1:


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

It should be {{pathFor 'singlePost' username=username _id=yourId}}

Your route should work then.



来源:https://stackoverflow.com/questions/23124450/iron-router-meteor-post-details-with-username-in-url

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