【问题标题】:Attaching multiple schemas to same collection using autoform and collection2使用 autoform 和 collection2 将多个模式附加到同一个集合
【发布时间】:2016-08-04 08:04:29
【问题描述】:

我有两种形式和两种模式,它们没有任何共同之处。但我仍然需要将它们存储在同一个集合中。

例如:

schema1 = new SimpleSchema({ field1, field2, field3 });
collection.attachSchema(schema1);

schema2 = new SimpleSchema({ fieldX, fieldY, fieldZ });
collection.attachSchema(schema2);

Collection2 documentation了解到,上述方法实际上会将两个模式合并为一个大模式。这意味着两个表单都必须具有属于两个架构的所有字段。

这意味着我不能有一个只有 schema1 的 autoForm 和另一个只有 schema2 的 autoform。

根据文档,我尝试实现 replace: true - 每次都会覆盖架构。 (至少我是这样理解的——它们不会合并到一个大模式中)

例如:

schema1 = new SimpleSchema({ field1, field2, field3 });
collection.attachSchema(schema1, {replace: true});

schema2 = new SimpleSchema({ fieldX, fieldY, fieldZ });
collection.attachSchema(schema2 {replace: true});

以上内容仍然无法解决问题,并且不知何故,模式仍然被合并。意思是,即使没有填写 fieldX 的规定,我仍然会收到 autoform1 中 FieldX 为空白的通知。

我还尝试了另一种使用变体的方法。

例如:

schema1 = new SimpleSchema({ field1, field2, field3 });
collection.attachSchema(schema1, {selector: {type: 'forForm1'}});

schema2 = new SimpleSchema({ fieldX, fieldY, fieldZ });
collection.attachSchema(schema2, {selector: {type: 'forForm2'}});

当我实现上述内容时,我收到一个自动生成错误,指出在处理多个模式时必须将参数传递给 doc。

我该如何做呢?

文档特别指出:

现在两个模式都已附加。当您插入文档时,键入: 'simple' 在文档中,它将仅针对 简单产品架构。当您插入文档时,类型:'variant' 在文档中,它将仅针对 VariantProductSchema。

我不知道我需要如何传递 doc = ????在模板中。有人可以指导我吗?

这是我的自动生成模板:

Form1:

{{#autoForm    collection = "pgTemplates"  type ="insert" doc= ???? id ="InsertForm1" }}
{{#each afFieldNames}}
{{> afQuickField name=this.name options = afOptionsFromSchema  }}
{{/each}}

Form2:

{{#autoForm    collection = "pgTemplates"  type ="insert" doc= ???? id ="InsertForm1" }}
{{#each afFieldNames}}
{{> afQuickField name=this.name options = afOptionsFromSchema  }}
{{/each}}

【问题讨论】:

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


    【解决方案1】:

    根据documentation 进行自动成型:

    doc:更新表单所必需的,并且必须至少有一个_id 财产。传递当前文档对象,通过调用来检索 findOne() 例如。对于插入表单,您也可以使用此 属性传递一个设置了默认表单值的对象(相同 效果就像在表单中的每个字段上设置一个 value 属性)。

    还有另一个参数schema在这种情况下可能有用:

    schema:如果未设置集合,则为必需。此架构将用于 在提交之前生成并验证表单,因此您可以指定 如果您想使用以下模式,则将其与集合一起使用 与您的收藏使用的略有不同。然而 最终对象仍然必须通过对集合的验证 架构。设置为以下之一:

    • 返回 SimpleSchema 实例的辅助函数的名称(不带引号)。
    • 窗口命名空间中的 SimpleSchema 实例的名称(用引号引起来)。

    所以你可以尝试在每种情况下根据你特定的schema设置架构参数,也可以尝试在doc中传递当前文档对象。

    我想模板看起来像:

    Form1:

    {{#autoForm  collection = "pgTemplates"  schema="schema1"  type ="insert" doc=docObject id ="InsertForm1" }}
      {{#each afFieldNames}}
        {{> afQuickField name=this.name options = afOptionsFromSchema1  }}
      {{/each}}
    

    Form2:

    {{#autoForm  collection = "pgTemplates"  schema="schema2"  type ="insert" doc=docObject id ="InsertForm1" }}
      {{#each afFieldNames}}
        {{> afQuickField name=this.name options = afOptionsFromSchema2  }}
      {{/each}}
    

    在上述情况下,您需要确保您的 schema 实例在窗口命名空间中正确可用

    或者你可以有一个帮助函数返回特定的实例,比如:

    有助手:

    {{#autoForm  collection = "pgTemplates"  schema=getSchema1  type ="insert" doc=docObject id ="InsertForm1" }}
        {{#each afFieldNames}}
          {{> afQuickField name=this.name options = afOptionsFromSchema2  }}
        {{/each}}
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-08-09
      • 1970-01-01
      • 2017-07-18
      • 1970-01-01
      • 2020-05-20
      • 1970-01-01
      • 2013-01-10
      • 1970-01-01
      相关资源
      最近更新 更多