使用Autoform for Meteor获取CommentId的PostId

时间:2015-01-11 09:21:51

标签: meteor meteor-autoform

如果没有autoform,我们通常会在下面提交评论:

评论-submit.js

 'submit form': function(e, template) {
     e.preventDefault();

     var $body = $(e.target).find('[name=body]');
     var status = {
       body: $body.val(),
       postId: template.data._id
     };

我们可以将postId注入每个评论中。

如何使用Autoform做到这一点?

我在评论集附近尝试了这个:

 Comments = new Mongo.Collection('comments');

 Comments.before.insert(function (userId, doc) {
   doc.postId = Posts.findOne()._id; 
 });



 Comments.attachSchema(new SimpleSchema({
   body: {
     type: String,
     autoform: {
       'label-type': 'placeholder',
       placeholder: 'Add your comment…'
     }
   },
   postId: {
     type: String
   }
 }));

这是有效的,但它总是得到集合中第一篇文章的postId,即使它实际上是第2,第3或第1篇以外的其他帖子。

请指导。感谢。

1 个答案:

答案 0 :(得分:0)

collection.findOne()始终返回插入到集合中的第一个文档。因此,我想到的最好的方法就是隐藏的领域。只需在autoForm

中添加一个字段即可
{{ > afQuickField name="postId" value=_id style="display: hidden;" }}

此解决方案无需任何挂钩或特殊属性即可使用。