Meteor.js - Using pathFor with a "foreign key"

I have a collection Postswhere everyone Postin the collection has a property userId. On my detailed message page, I want to associate a username with a link to their profile using an assistant pathFor. If I just paste {{pathFor 'userProfile'}}, it establishes a connection with Post _id, as expected, but I obviously need the userId in the link.

I tried to create a second data context in the template helper, but that didn't work either.

Script:

Template.postPage.helpers({
    user: function () {
        return {_id: this.userId};
    }      
});

Template:

<template name="postPage">
    {{#with user}}<a href="{{pathFor 'userProfile'}}">{{/with}}{{author}}</a>
</template>

How can I use pathForwith a field userIdfrom my Postdoc instead of a field _idfrom my Postdoc?

I use an iron router if that matters.

+4
2

, flummoxed, Iron Router pathFor, :

Router.map(function () {
  this.route('postShow', {
    path: '/posts/:_id'
  });
});

, :_id . :

Router.map(function () {
  this.route('userProfile', {
    path: '/users/:userId'
  });
});

:userId userId Post.

#with. :

<template name="postPage">
  <a href="{{pathFor 'userProfile'}}">{{author}}</a>
</template>

userProfile Post , _id, userId, author . , , :userId, :_id :author - .

+1

, iron-router 0.8.0, :

<template name="postPage">
  <a href="{{pathFor 'userProfile' params=this}}">{{author}}</a>
</template>

Per https://github.com/EventedMind/iron-router/blob/master/lib/client/ui/helpers.js#L42:

/**
 * Example Use:
 *
 *  {{pathFor 'items' params=this}}
 *  {{pathFor 'items' id=5 query="view=all" hash="somehash"}}
 *  {{pathFor route='items' id=5 query="view=all" hash="somehash"}}
*/
+1

All Articles