【问题标题】:Add postId to comments with autoform in Meteor在 Meteor 中使用 autoform 将 postId 添加到评论中
【发布时间】:2015-09-22 15:32:24
【问题描述】:

使用meteor-autoform时如何将postId添加到cmets?

我试过了

AutoForm.hooks({
  insertCommentForm: {
    formToDoc: function(doc) {
      doc.postId = this.formAttributes.parentContext._id;
      return doc;
    },
  }
});

AutoForm.hooks({
  insertCommentForm: {
    formToDoc: function(doc) {
      doc.postId = Template.parentData(1)._id;
      return doc;
    },
  }
});

AutoForm.hooks({
  insertCommentForm: {
    before: {
      method: function(doc) {
        doc.postId = this.formAttributes.parentContext._id;
        return doc;
      }
    }
  }
});

AutoForm.hooks({
  insertCommentForm: {
    before: {
      method: function(doc) {
        doc.postId = Template.parentData(1)._id;
        return doc;
      }
    }
  }
});

但无论我做什么,postId 都是未定义的。

编辑

我是这样使用的:

<template name="comment">
  <div>
    <h1>{{_id}} {{title}}</h1>
    {{#if currentUser}}
      {{> quickForm collection="Comments" id="insertCommentForm" type="insert"}}
    {{/if}}
    ....

所以_id 应该可以访问。

编辑 2

我已经试过了

before: {
  insert: function(doc, template) {
    doc.postId = Template.instance().post._id;
    console.log(doc);
    return doc;
  }
},

在我使用的模板中

{{> quickForm collection="Comments" id="insertCommentForm" type="insert" post=this template="bootstrap3-inline" label-class="sr-only"}}

但帖子是undefined,所以我收到错误Uncaught TypeError: Cannot read property '_id' of undefined

【问题讨论】:

  • 如果您在那里尝试,您是否能够在“父模板”上记录/访问 _id 的值?这将确保您的属性 _id 按预期可用。
  • 我已经更新了我的问题。变量 _id 是可访问的,所以我想我应该能够将它加载到 autoform 钩子中。

标签: javascript node.js meteor meteor-autoform


【解决方案1】:

改为使用您的

{{> quickForm collection="Comments" id="insertCommentForm" type="insert"}}

试试吧

{{> quickForm collection="Comments" id="insertCommentForm" type="insert" postId=_id}}

然后尝试通过

在助手内部访问这个值

Template.instance().data.postId


您也可以将整个帖子对象发送到子模板,如

{{> quickForm collection="Comments" id="insertCommentForm" type="insert" post=this}}

然后通过

拥有对该集合文档的完全访问权限

(例如)

Template.instance().data.post._id


这是一个通过模板访问数据的小示例

http://meteorpad.com/pad/Ke9DJnbvtsqjSHJy2/SimpleDataGivenThroughTemplates

【讨论】:

  • 对不起,我错过了 data 属性 - 你必须使用 Template.instance().data。 ...请参阅更新的答案。
猜你喜欢
  • 2015-03-09
  • 1970-01-01
  • 2017-04-21
  • 1970-01-01
  • 2023-03-29
  • 2015-11-29
  • 1970-01-01
  • 2021-12-30
  • 2018-12-23
相关资源
最近更新 更多