【发布时间】: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