使用一个 autoForm,我需要将数据插入两个 collections

Using one autoForm, I need to insert data into two collections

我目前正在开发一个库存系统,该系统将零件 Collection 和采购 Collection 作为应用程序的 backbone。每个部分都有相应的采购。 I.E 零件必须具有与其关联的零件 ID、序列号和成本号。我将 Meteor.js 与 coffeescrip、jade 和 Graphr 一起使用。我可以单独插入每个 collection,但它们似乎没有连接。我已经在两个连接之间设置了 link 用户,但我有点迷失下一步该去哪里

这是 collection

的片段

购买Collection

    PurchaseInventory.schema = new SimpleSchema
        partId:
            type:String
            optional:true

        serialNum:
            type:Number
            optional:true

        costNum:
            type:Number
            optional:true

部分Collection/schema


    Inventory.schema = new SimpleSchema

        name:
            type:String
            optional:true

        manufacturer:
            type:String
            optional:true

        description:
            type:String
            optional:true

零件查询


    export getInventory = Inventory.createQuery('getInventory',
        $filter: ({ filters, options, params }) ->
                if params.filters then Object.assign(filters, params.filters)
                if params.options then Object.assign(options, params.options)
                return { filters, options , params }
        name:1
        manufacturer:1
        description:1
        pic:1
        purchase:
            partId:1

    )

购买查询


    export getPurchase = PurchaseInventory.createQuery('getPurchase',
        $filter: ({ filters, options, params }) ->
                if params.filters then Object.assign(filters, params.filters)
                if params.options then Object.assign(options, params.options)
                return { filters, options , params }
        serial:1
        cost:1
        date:1
        warrentyDate:1
        userId:1
        )

链接器

//Parts
    Inventory.addLinks
        purchase:
            collection:PurchaseInventory
            inversedBy:"part"


    //purchases
    PurchaseInventory.addLinks
        part:
            type:'one'
            collection:Inventory
            field:'partId'
            index: true

最后 Jade/Pug 自动表单


    +autoForm(class="inventoryForm" schema=schema  id="inventoryInsertForm" validation="blur" type="method" meteormethod="inventory.insert")
        .formGroup
          +afQuickField(name="name" label="Name")
          +afQuickField(name="manufacturer" label="Manufacturer")
          +afQuickField(name="description" label="Description")
          button#invenSub(type="submit") Submit

重申一下,我的目标是让零件中的每个项目都有一个 link 到其相应的购买数据。

最直接的方法是使用autoform form type normal and create a custom event handler for the submit event (alternatively you can use the AutoForm hooks onSubmit). From there you can use the AutoForm.getFormValues API function获取当前文档。

因为我不喜欢 Coffeescript,所以我会提供以下 Blaze/JS 代码,但我认为它应该给你一个想法:

{{# autoForm type="normal" class="class="inventoryForm" schema=schema  id="inventoryInsertForm" validation="blur"" schema=schema  id="insertForm" validation="blur" }}
  <!-- your fields -->
{{/autoForm}}
/**
 * validates a form against a given schema and returns the
 * related document including all form data.
 * See: https://github.com/aldeed/meteor-autoform#sticky-validation-errors
 **/
export const formIsValid = function formIsValid (formId, schema) {
  const { insertDoc } = AutoForm.getFormValues(formId)
  // create validation context
  const context = schema.newContext()
  context.validate(insertDoc, options)

  // get possible validation errors
  // and attach them directly to the form
  const errors = context.validationErrors()
  if (errors && errors.length > 0) {
    errors.forEach(err => AutoForm.addStickyValidationError(formId, err.key, err.type, err.value))
    return null
  } else {
    return insertDoc
  }
}
Template.yourFormTempalte.events({
  'submit #insertForm' (event) {
    event.preventDefault() // important to prevent from reloading the page!


    // validate aginst both schemas to raise validation
    // errors for both instead of only one of them
    const insertDoc = formIsValid('insertForm', PurchaseInventory.schema) && formIsValid('insertForm', Inventory.schema)

    // call insert method if both validations passed
    Meteor.call('inventory.insert', insertDoc, (err, res) => { ... })
    Meteor.call('purchaseInventory.insert', insertDoc, (err, res) => { ... })
  }
})

请注意,如果您需要在服务器端成功执行两次插入操作,您应该编写第三个 Meteor 方法,在一次方法调用中显式地在两个集合中插入一个文档。如果你有 Mongo 版本 >= 4 你可以将它与 transactions.

结合起来