Meteor:自定义AutoForm与对象数组

Pau*_*ery 10 arrays object meteor

我有一个SimpleSchema,它包含一个对象数组:

Things.attachSchema( new SimpleSchema({
    name: {
        type: String,
        label: "Name",
        max: 50
    },
    fields: { 
        type: [Object],
    },
    fields.$.name: {
        type: String
    },
    fields.$.amount: {
        type: Number
    }
}) )
Run Code Online (Sandbox Code Playgroud)

我正在尝试使用afEachArrayItem创建自定义表单,但我无法弄清楚如何引用数组中每个对象的属性.

我的模板看起来像这样(删除了html):

{{#autoForm collection="things" id="myForm" }}
    {{> afQuickField name='schemaName'}}

    {{#afEachArrayItem name="fields"}}

        {{> afFieldInput name="name"}  
        {{> afFieldInput name="amount"}

    {{/afEachArrayItem}}

{{/autoForm}}
Run Code Online (Sandbox Code Playgroud)

什么应该传递给afFieldInputs中的"name"?

Pau*_*ery 12

要访问数组中对象的字段,您可以使用:

this.current
Run Code Online (Sandbox Code Playgroud)

因此,要修复上面给出的示例,请使用:

{{#autoForm collection="things" id="myForm" }}
    {{> afQuickField name='schemaName'}}

    {{#afEachArrayItem name="fields"}}

        {{> afFieldInput name=this.current.name}}  
        {{> afFieldInput name=this.current.amount}}

    {{/afEachArrayItem}}

{{/autoForm}}
Run Code Online (Sandbox Code Playgroud)

我不知道这是否是正确的方法,但它似乎工作.


rhl*_*hrm 5

您可以添加按钮来添加/删除数组项,如下所示:

{{#autoForm collection="things" id="myForm" }}
    {{> afQuickField name='schemaName'}}

    {{#afEachArrayItem name="fields"}}

        <button type="button" class="btn btn-primary autoform-remove-item"><span class="glyphicon glyphicon-minus"></span></button>
        {{> afFieldInput name=this.current.name}}  
        {{> afFieldInput name=this.current.amount}}

    {{/afEachArrayItem}}
    <button type="button" class="btn btn-primary autoform-add-item" data-autoform-field="fields"><span class="glyphicon glyphicon-plus"></span></button>

{{/autoForm}}
Run Code Online (Sandbox Code Playgroud)

这将使用 AutoForm 的内置按钮和图标,因此可以根据需要随意修改 HTML。