Iron Router / Meteor - Post details with username in URL -
i'm relatively new meteor (especially iron router), , have been stuck on following issue...
i have route displays details single post:
this.route('singlepost',{ path:'/posts/:_id', data:function(){ return posts.findone(this.params._id); } }); this works fine, i'd able show post owner's username in url, rather static "/posts/" path, ex:
this.route('singlepost',{ path:'/:username/:_id', data:function(){ return posts.findone(this.params._id); } }); the post object includes user id of owner, not username (username in meteor.users collection).
when try set route 2 dynamic values (username, post id), pathfor link disappears (i assume because cannot find "username" in post object returned).
how can route recognize username? assume lookup function users collection i'm not sure when/where. also, how able validate route make sure post owned correct username?
edit - here 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 list , details views, cannot pathfor link include username.
looking @ template, appear not passing username or id in {{pathfor 'singlepost'}}.
it should {{pathfor 'singlepost' username=username _id=yourid}}
your route should work then.
Comments
Post a Comment