简体   繁体   English

使用autoform和collection2将多个架构附加到同一集合

[英]Attaching multiple schemas to same collection using autoform and collection2

I have two forms and two schema that don't have anything in common. 我有两种形式和两种架构,它们没有任何共同之处。 But I still need them to be stored in the same collection. 但是我仍然需要将它们存储在同一集合中。

Eg.: 例如。:

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

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

From Collection2 documentation it is understood that the above method will actually merge both the schema into a single big schema. Collection2文档中可以理解,上述方法实际上会将两个模式合并为一个大模式。 This means that both form must have all fields belonging to both schema. 这意味着两种形式都必须具有属于两种模式的所有字段。

This means that I can't have an autoForm with just schema1 and another autoform with just schema2. 这意味着我不能拥有仅具有schema1的自动表单和仅具有schema2的另一个自动表单。

As per the documentation, I tried implementing replace: true - by which, the schema gets overwritten each time. 根据文档,我尝试实现replace:true-每次,架构都会被覆盖。 ( At least this is how I understand it - they don't get merged into a big schema) (至少这是我的理解方式-它们不会合并为一个大架构)

Eg: 例如:

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

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

The above still does not fix the issue and Somehow, the schemas still get merged. 上面的内容仍然无法解决问题,并且仍然以某种方式合并了架构。 Meaning, I still get notified that FieldX is blank in autoform1 even though there is no provision for fieldX to be filled. 意思是,即使没有提供要填充的fieldX,我仍然会收到通知,在autoform1中FieldX为空白。

I Also tried the other approach where in you use variations. 我还尝试了使用变体的其他方法。

Eg.: 例如。:

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

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

When I implement the above, I get an autoform error saying that an argument to doc must be passed when dealing with multiple schema. 当我实现以上内容时,我收到一个自动套用错误,该错误指示在处理多个架构时必须传递doc参数。

How exactly do I do this? 我到底该怎么做?

THe documentation specifically states : 该文档特别指出:

Now both schemas are attached. 现在,两个模式都已附加。 When you insert a document where type: 'simple' in the document, it will validate against only the SimpleProductSchema. 当您在文档中插入类型为“ simple”的文档时,它将仅针对SimpleProductSchema进行验证。 When you insert a document where type: 'variant' in the document, it will validate against only the VariantProductSchema. 当您在文档中插入类型为“ variant”的文档时,它将仅针对VariantProductSchema进行验证。

I Don't know how I need to pass doc = ???? 我不知道我该如何传递doc = ???? in the template. 在模板中。 Could someone guide me? 有人可以指导我吗?

This is my autoform template: 这是我的自动表单模板:

Form1: 表格1:

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

Form2: 表格2:

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

According to the documentation for autoform: 根据有关自动套用的文档

doc : Required for an update form, and must have at least an _id property. doc :更新表单所必需,并且必须至少具有_id属性。 Pass the current document object, retrieved with a call to findOne() for example. 传递当前文档对象,例如,通过调用findOne()进行检索。 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). 对于插入表单,还可以使用此属性来传递设置了默认表单值的对象(与在表单中的每个字段上设置value属性的效果相同)。

There is also another parameter schema that may be of use in this case: 在这种情况下,还可以使用另一个参数schema

schema : Required if collection is not set. schema :如果未设置collection,则为必需。 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. 返回SimpleSchema实例的辅助函数的名称(不带引号)。
  • The name (in quotation marks) of a SimpleSchema instance that is in the window namespace. 窗口名称空间中的SimpleSchema实例的名称(用引号引起来)。

So you can try setting the schema parameter according to your specific schema in each case, and also try to pass the current document object in doc . 因此,您可以尝试根据每种情况的特定schema设置schema参数,还可以尝试在doc传递当前文档对象。

I suppose the template would look like: 我想模板看起来像:

Form1: 表格1:

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

Form2: 表格2:

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

In the above case, you will need to make sure that your schema instances are available properly in the window namespace 在上述情况下,您需要确保schema实例在窗口名称空间中正确可用

Or you can have a helper function returning the specific instance, like: 或者,您可以使用一个辅助函数来返回特定的实例,例如:

With helper: 有帮手:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM