如何在帮助器或事件中检索对象的内部_id?

时间:2016-02-11 20:44:30

标签: javascript meteor meteorite flow-router

我正在尝试添加评论,其字段为{postId}。目前我返回帖子ID以将其添加到评论集合var id = FlowRouter.getParam('id');,因为我的路线包含特定帖子的ID我将在参数中留下评论。现在我想改变路线而不是myapp.com/posts/:id到myapp.com/post/:title所以我不能再从param获取id了....我怎么还能得到ID在路由器params中不再可用的帖子?

现在我的事件代码是:

Template.post.events({
  "click #submitComment": function () {
      var commentText = $("#commentfield").val();
     var postId = FlowRouter.getParam('id');` 
     Comments.insert({postId: postId, comment: commentText});
  } 

})

我记得其他应用程序我使用template.data._id这样的东西,但我无法让它工作

这个问题也适用于我的帮助者和订阅者,我将始终需要获取模板的具体帖子,评论,视频ID ......但需要找到一种方法,因为我不希望我的所有路线都包含对他们的ids。 感谢

2 个答案:

答案 0 :(得分:0)

您缺少将模板变量传递到click事件:

 "click #submitComment" : function (event, template) { ....

注意(event, template)。现在,在活动中,按照您的建议使用template.data._id

答案 1 :(得分:0)

如果我正确理解了这一点,那么您正在改变从拥有帖子ID到拥有标题字段的路线。看起来你也知道#with。由于您的助手名为posts,因此以下是一些我认为应该有效的示例代码:

Template.post.helpers({
    posts() {
        var post = Posts.findOne({title: FlowRouter.getParam('title')});
        if(post) {
            return post;
        }
    }
});

然后,我相信您的事件代码可以保持不变。