在 Meteor 中使用自动表单将 postId 添加到评论中
Add postId to comments with autoform in Meteor
如何在使用 meteor-autoform
时 link 添加 postId
评论?
我试过了
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"}}
但是 post 是 undefined
所以我得到错误 Uncaught TypeError: Cannot read property '_id' of undefined
.
而是使用您的
{{> quickForm collection="Comments" id="insertCommentForm" type="insert"}}
试试
{{> quickForm collection="Comments" id="insertCommentForm" type="insert" postId=_id}}
然后尝试通过
访问助手内部的这个值
Template.instance().data.postId
您也可以将整个 post 对象发送到子模板,如
{{> quickForm collection="Comments" id="insertCommentForm" type="insert" post=this}}
然后可以通过
完全访问该集合文档
(例如)
Template.instance().data.post._id
这是一个通过模板访问数据的小示例
http://meteorpad.com/pad/Ke9DJnbvtsqjSHJy2/SimpleDataGivenThroughTemplates
如何在使用 meteor-autoform
时 link 添加 postId
评论?
我试过了
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"}}
但是 post 是 undefined
所以我得到错误 Uncaught TypeError: Cannot read property '_id' of undefined
.
而是使用您的
{{> quickForm collection="Comments" id="insertCommentForm" type="insert"}}
试试
{{> quickForm collection="Comments" id="insertCommentForm" type="insert" postId=_id}}
然后尝试通过
访问助手内部的这个值Template.instance().data.postId
您也可以将整个 post 对象发送到子模板,如
{{> quickForm collection="Comments" id="insertCommentForm" type="insert" post=this}}
然后可以通过
完全访问该集合文档(例如)
Template.instance().data.post._id
这是一个通过模板访问数据的小示例
http://meteorpad.com/pad/Ke9DJnbvtsqjSHJy2/SimpleDataGivenThroughTemplates