如何通过一个会话befor路由与铁:流星路由器?
在我post_item.html我得到这一行:如何通过一个会话befor路由与铁:流星路由器?
<a href="{{pathFor 'postPage' _id=this._id}}" class="discuss btn">Discuss</a>
在我router.js:
Router.map(function() {
this.route('postPage',{path:'/posts/:_id',
beforeRoutingTo: function(id) {
console.log("hello!");
Session.set('currentPostId', id);
}
});
});
在我post_page.js
Template.postPage.helpers({
currentPost: function() {
return Posts.findOne(Session.get('currentPostId'));
}
});
在我post_page.html
<template name="postPage">
{{#with currentPost}}
{{> postItem}}
{{/with}}
</template>
我做错了什么? 如何将ID传递到新的“页面”并显示结果?
我假设你正在使用本书'Discover Meteor'开发流星教程。然而,我面临同样的问题,我无需使用会话即可解决问题。如果您使用iron router,则不再需要任何助手。
客户端/ router.js:
Router.map(function() {
this.route('/post/:_id', function() {
var item = Posts.findOne({_id: this.params._id});
this.render('postItem', {data: item});
},
{
name: 'post.show'
});
});
客户端/职位/ post_item.html
<a href="{{pathFor route='post.show'}}" class="discuss btn">Discuss</a>
如你所见,在router.js
的ID从URL解析。此外,我们将其命名“posts.show”后/post/:id
路线,我们可以从引用该名称的看法得到的网址:{{pathFor route='post.show'}}
不知道为什么你需要这个会话,但你的路线看起来不对。尝试下面的方法:
Router.map(function() {
this.route('postPage',{path:'/posts/:_id',
onBeforeAction: function() {
console.log("hello!");
Session.set('currentPostId', this.params._id);
}
});
});
这是'this.params._id' – fuzzybabybunny
感谢。更新了 – FullStack
WOW!那真的有用!我尝试了很长一段时间很多不同的类型... 我想这样做更多,但它不工作: 'Router.route('/ post /:_ id',{ name:'postPage' , 数据:功能(){ 返回 Posts.findOne(this.params._id);} });' 在post_item.html: ''Discuss 而在post_page.html '
只是在这种情况下,我想显示更多的只是项目,我需要post_pa ge.html用于向该单击的项目添加更多详细信息。 我只有老书“发现流星” - 我看到了这本书的版本(用德语) - http://de.discovermeteor.com/chapters/routing/ – Suisse
OK thx!我测试了一下你的代码并将其改为: route.js: 'this.route('/ post /:_ id',function(){ var item = Posts.findOne({_ id:this.params ._id}); this.render( 'postPage',{数据:项});} , { 名: 'postPage' });' 在post_item。HTML的讨论链接: ''Discuss在 我post_page.html可以添加更多的详细信息: '