【问题标题】:Getting PostId for CommentId using Autoform for Meteor使用 Autoform for Meteor 获取 CommentId 的 PostId
【发布时间】:2015-03-09 06:01:52
【问题描述】:

如果不使用自动表单,通常情况下,我们将执行以下操作来提交评论:

comment-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,即使它实际上是第二个、第三个或不是第一个帖子。

请指导。谢谢。

【问题讨论】:

    标签: meteor meteor-autoform


    【解决方案1】:

    collection.findOne() 总是返回插入到集合中的第一个文档。所以我想到的最好的方法是隐藏字段。只需在您的autoForm 中添加一个字段

    {{ > afQuickField name="postId" value=_id style="display: hidden;" }}
    

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

    【讨论】:

      猜你喜欢
      • 2015-09-22
      • 1970-01-01
      • 1970-01-01
      • 2014-12-11
      • 2016-01-24
      • 2015-09-20
      • 2015-02-28
      • 1970-01-01
      • 2015-12-31
      相关资源
      最近更新 更多