首先,只要我们在同一页面上,如果您的路线设置如下:
FlowRouter.route('/blog/:postId', {
action: function (params, queryParams) {
FlowLayout.render('layout', { body: 'postTemplate' });
},
});
您可以FlowRouter.getParam('postId')
从 AutoForm 挂钩内部调用
您需要使用 AutoForm 挂钩并拥有完整的架构。我正在使用该包aldeed:collection2
来设置架构。该postId
字段必须明确声明。此代码在服务器和客户端上运行。
Comments = new Mongo.Collection("comments");
Comments.attachSchema(new SimpleSchema({
comment: {
type: String,
label: "Comment"
},
postId: {
type: String
}
}));
像这样设置表单不是您想要的:
{{> quickForm collection="Comments" id="commentForm" type="insert"}}
这不好,因为它会postId
在 HTML 输出中显示该字段。我们不希望这样,因此您必须像这样完全定义表单:
{{#autoForm collection="Comments" id="commentForm" type="insert"}}
<fieldset>
{{> afQuickField name='comment' rows=6}}
</fieldset>
<button type="submit" class="btn btn-primary">Insert</button>
{{/autoForm}}
然后添加 AutoForm 钩子。此代码在客户端上运行。
var commentHooks = {
before: {
insert: function(doc){
var postId = FlowRouter.getParam('postId');
doc.postId = postId;
return doc;
}
}
};
AutoForm.addHooks(['commentForm'],commentHooks);
确保您设置了允许/拒绝规则,并且它应该可以正常工作。