如何通过Meteor中的隐藏字段将_id传递给autoForm

How to pass _id to autoForm via hidden field in Meteor?

本文关键字:id autoForm 字段 Meteor 何通过 隐藏      更新时间:2023-09-26

我有一个项目集合和Meteor.users集合。我的目标是将项目的document_id添加到用户名中。

HTML:

<template name="insertName">
    {{#each projectList}}
        {{> projectView}}
    {{/each}}
</template>
<template name="projectView">
    {{#autoForm collection="Meteor.users" id="insertUser" type="insert"}}
         {{> afQuickField name="username"}}
         // From here I can access the _id property from the projects collection
         // Question: How do I pass it to my form without creating an input field?
    {{/autoForm}}
</template>

JS:

Meteor.users = new Meteor.Collection('projects');
Meteor.users.attachSchema(new SimpleSchema({
    username: {
        type: String,
        max: 50
    },
    projectId: {
        type: String,
        max: 20
        // Use autoValue to retrieve _id from projects collection?
    }
});

使用上面的HTML代码,我知道我可以写这个:

{{> afQuickField name='projectId' value=this._id}}

然而,这会创建一个我不想要的输入字段。如何在没有可见输入字段的情况下将_id传递给我的autoForm?或者有人可以给我指一个不同的方向?

有多种方法可以解决此问题,但基本上,如果您想要隐藏字段,则必须将自动表单字段的类型设置为隐藏。

你可以做任何一个:

{{> afQuickField name='projectId' value=this._id type='hidden'}}

或者在模式本身上这样做

Meteor.users = new Meteor.Collection('projects');
Meteor.users.attachSchema(new SimpleSchema({
    username: {
        type: String,
        max: 50
    },
    projectId: {
        type: String,
        max: 20,
        autoform: {
          type: 'hidden'
        }
    }
});

我不相信这是最好的解决方案,但我认为这是一种回避问题的方式:

{{> afQuickField name='projectId' value=this._id type='hidden'}}