迁移到FlowRouter需要类似于模板数据上下文的东西

所以我读了很多关于Iron Router vs FlowRouter的讨论。

我使用Iron Router开始了我的项目,但是由于改变了主意,我正在迁移到FlowRouter。

一切都很顺利,直到我开始迁移我的应用程序的评论部分。 你看,这个部分在应用程序上被多次重复使用,它作为新闻,文章,照片,video等的评论部分。

使用IR的数据上下文的例子:

Router.route('/news/:slug', { name: 'newsItem', waitOn: function() { Meteor.subscribe('news.single', this.params.slug) }, data: function() { return News.findOne({slug: this.params.slug}); } }); <template name="newsItem"> <p>{{title}}</p> <p>{{body}}</p> {{> commentSection}} </template> 

评论集合模式有一个“types”(要知道这个评论属于什么types的“东西”,新闻,照片等)。 该types在commentSection模板的“form .submit”事件上设置。 例:

 'submit form': function(e, template) { e.preventDefault(); var $body = $(e.target).find('[name=body]'); console.log(template.data.type); var comment = { type: template.data.type, parentId: template.data._id, parentSlug: template.data.slug, body: $body.val() }; Meteor.call('insertComment', comment, function(error, commentId) { if (error){ alert(error.reason); } else { $body.val(''); } }); } 

这工作,因为模板数据上下文包含新闻项目,而这又有一个types属性。

我如何才能实现类似于这个只使用stream量路由器没有设置模板上的数据,因为它是由官方指导build议?

您可能需要使用模板订阅和{{#with}}助手。

 Template.newsItem.onCreated( function() { Template.instance().subscribe('news.single', FlowRouter.current().params.slug); }); Template.newsItem.helpers({ item() { let item = News.findOne(); if( item ) { return item; } } }); <template name="newsItem"> {{#with item}} <!-- Your existing stuff --> {{/with}} </template>