【问题标题】:How to pass _id to autoForm via hidden field in Meteor?如何通过 Meteor 中的隐藏字段将 _id 传递给 autoForm?
【发布时间】:2016-01-31 21:02:42
【问题描述】:

我有一个项目集合和 Meteor.users 集合。我的目标是将项目的文档_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?或者也许有人可以为我指明不同的方向?

【问题讨论】:

标签: javascript meteor meteor-autoform meteor-collection2 simple-schema


【解决方案1】:

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

你可以这样做:

{{> 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'
        }
    }
});

【讨论】:

    【解决方案2】:

    我不确定这是最好的解决方案,但我认为这是解决您遇到的问题的一种方式:

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

    【讨论】:

      猜你喜欢
      • 2016-12-14
      • 2011-11-02
      • 1970-01-01
      • 1970-01-01
      • 2015-03-03
      • 1970-01-01
      • 2014-08-14
      • 1970-01-01
      • 2015-09-12
      相关资源
      最近更新 更多