使用 autoform 和 collection2 将多个模式附加到同一个集合
Attaching multiple schemas to same collection using autoform and collection2
我有两种形式和两种模式,它们没有任何共同之处。但我仍然需要将它们存储在同一个集合中。
例如:
schema1 = new SimpleSchema({ field1, field2, field3 });
collection.attachSchema(schema1);
schema2 = new SimpleSchema({ fieldX, fieldY, fieldZ });
collection.attachSchema(schema2);
从Collection2 documentation了解到,上述方法实际上会将两个模式合并为一个大模式。这意味着两种形式都必须具有属于两种模式的所有字段。
这意味着我不能有一个只有 schema1 的自动表单和另一个只有 schema2 的自动表单。
根据文档,我尝试实施 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 的参数。
我该怎么做?
文档明确指出:
Now both schemas are attached. When you insert a document where type:
'simple' in the document, it will validate against only the
SimpleProductSchema. When you insert a document where type: 'variant'
in the document, it will validate against only the
VariantProductSchema.
我不知道如何通过 doc = ????在模板中。有人可以指导我吗?
这是我的自动表单模板:
表格 1:
{{#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}}
根据 documentation for autoform:
doc
: Required for an update form, and must have at least an _id
property. Pass the current document object, retrieved with a call to
findOne()
for example. For an insert form, you can also use this
attribute to pass an object that has default form values set (the same
effect as setting a value attribute on each field within the form).
还有另一个参数schema
在这种情况下可能有用:
schema
: Required if collection is not set. This schema will be used to
generate and validate the form prior to submission, so you can specify
this along with a collection if you want to use a schema that is
slightly different from the one your collection uses. However, the
final object will still have to pass validation against the collection
schema. Set to one of the following:
- The name of a helper function (no quotation marks) that returns an instance of SimpleSchema.
- The name (in quotation marks) of a SimpleSchema instance that is in the window namespace.
因此您可以尝试在每种情况下根据您的特定 schema
设置架构参数,并尝试在 doc
.
中传递当前文档对象
我想模板应该是这样的:
表格 1:
{{#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
实例在 window 命名空间
中正确可用
或者你可以有一个返回特定实例的辅助函数,比如:
有帮手:
{{#autoForm collection = "pgTemplates" schema=getSchema1 type ="insert" doc=docObject id ="InsertForm1" }}
{{#each afFieldNames}}
{{> afQuickField name=this.name options = afOptionsFromSchema2 }}
{{/each}}
我有两种形式和两种模式,它们没有任何共同之处。但我仍然需要将它们存储在同一个集合中。
例如:
schema1 = new SimpleSchema({ field1, field2, field3 });
collection.attachSchema(schema1);
schema2 = new SimpleSchema({ fieldX, fieldY, fieldZ });
collection.attachSchema(schema2);
从Collection2 documentation了解到,上述方法实际上会将两个模式合并为一个大模式。这意味着两种形式都必须具有属于两种模式的所有字段。
这意味着我不能有一个只有 schema1 的自动表单和另一个只有 schema2 的自动表单。
根据文档,我尝试实施 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 的参数。
我该怎么做?
文档明确指出:
Now both schemas are attached. When you insert a document where type: 'simple' in the document, it will validate against only the SimpleProductSchema. When you insert a document where type: 'variant' in the document, it will validate against only the VariantProductSchema.
我不知道如何通过 doc = ????在模板中。有人可以指导我吗?
这是我的自动表单模板:
表格 1:
{{#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}}
根据 documentation for autoform:
doc
: Required for an update form, and must have at least an_id
property. Pass the current document object, retrieved with a call tofindOne()
for example. For an insert form, you can also use this attribute to pass an object that has default form values set (the same effect as setting a value attribute on each field within the form).
还有另一个参数schema
在这种情况下可能有用:
schema
: Required if collection is not set. This schema will be used to generate and validate the form prior to submission, so you can specify this along with a collection if you want to use a schema that is slightly different from the one your collection uses. However, the final object will still have to pass validation against the collection schema. Set to one of the following:
- The name of a helper function (no quotation marks) that returns an instance of SimpleSchema.
- The name (in quotation marks) of a SimpleSchema instance that is in the window namespace.
因此您可以尝试在每种情况下根据您的特定 schema
设置架构参数,并尝试在 doc
.
我想模板应该是这样的:
表格 1:
{{#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
实例在 window 命名空间
或者你可以有一个返回特定实例的辅助函数,比如:
有帮手:
{{#autoForm collection = "pgTemplates" schema=getSchema1 type ="insert" doc=docObject id ="InsertForm1" }}
{{#each afFieldNames}}
{{> afQuickField name=this.name options = afOptionsFromSchema2 }}
{{/each}}