【问题标题】:Using one autoForm, I need to insert data into two collections使用一个 autoForm,我需要将数据插入到两个集合中
【发布时间】:2020-05-20 17:13:47
【问题描述】:

我目前正在开发一个库存系统,该系统采用 Part Collection 和 Purchase Collection 作为应用程序的主干。每个零件多有相应的采购。即,零件必须具有与其关联的零件 ID、序列号和成本号。我正在将 Meteor.js 与咖啡脚本、玉石和 Graphr 一起使用。我可以单独插入每个集合,但它们似乎没有连接。我已经在两个连接之间设置了链接器,但我对下一步去哪里有点迷茫

这是一个集合的sn-p

购买集合

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

        serialNum:
            type:Number
            optional:true

        costNum:
            type:Number
            optional:true

部件集合/模式


    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

重申我的目标是让每个项目都具有指向其相应购买数据的链接。

【问题讨论】:

    标签: mongodb meteor coffeescript pug meteor-autoform


    【解决方案1】:

    最直接的方法是使用autoform form type normal 并为提交事件创建自定义事件处理程序(或者您可以使用AutoForm 挂钩onSubmit)。从那里您可以使用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 结合使用。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-07-21
      • 1970-01-01
      • 1970-01-01
      • 2016-03-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多