通过 route.js 分配 :_id 以在 template.js - Meteor 中使用它

assign :_id via route.js to use it in template.js - Meteor

我有 2 个 Collection:

Collection 1: Categories

Collection 2: Posts - category_id 所以 Category 有一些 Posts 与相同的 category_id.

我有一个模板,可以在其中查看我的所有类别,现在我想单击一个类别以查看其帖子,其中 category_id:_id

route.js:

    this.route('postsList', {
    path: '/category/:_id',
    data: function() {
        return myCategories.findOne(this.params._id);
    }

现在我知道我选择了哪个类别,但我不知道如何在我的 template.js 中获取 _id 来执行类似的操作:

    Template.postsList.helpers({
        drinks: function(){
            return Posts.find({id:_id});
        }
});

问题是我想从我的路线获取 :_id 到我的 template.js 以使用它。 this.params._id 在我的 template.js 中不适合我。

您可以将类别 ID 从 html 模板传递给您的助手。

您需要将 _id 从 html 传递给辅助函数 drinks

像这样-

{{drinks _id}}

而不仅仅是

{{drinks}}

现在,在 helper 中获取 id 作为参数

drinks: function(id){
  // here you have the id passed from html, do whatever you want

}

希望对您有所帮助。