จะเพิ่มความสัมพันธ์หรือการอ้างอิงกับ AutoForm ใน Meteor ได้อย่างไร

ฉันใช้ meteor-autoform เพื่อแทรกเอกสารในคอลเลกชัน Items ของฉันมีฟิลด์ groupId ฉันจะแทรกรหัสกลุ่มนี้ได้อย่างไรเมื่อฉันส่งแบบฟอร์มรายการสินค้า

<template name="itemForm">
  {{#autoForm type="insert" collection=Collections.Items}}
    {{> afQuickField name="name"}}
    <div class="form-group">
      <button type="submit" class="btn btn-primary">Add item</button>
      <button type="reset" class="btn btn-default">Reset Form</button>
    </div>
  {{/autoForm}}
</template>

ฉันสามารถสร้างฟิลด์อื่นที่มีรหัสกลุ่มของฉันได้ แต่ฉันไม่ต้องการให้ผู้ใช้เห็นฟิลด์นี้

ฉันจะตั้งค่า groupId "เบื้องหลัง" ได้อย่างไร


person Jamgreen    schedule 17.12.2014    source แหล่งที่มา
comment
ในฟิลด์ของคุณ คุณสามารถเพิ่ม autoform: {omit: true} เพื่อซ่อนฟิลด์ในแบบฟอร์มได้   -  person Mustafa    schedule 28.03.2015


คำตอบ (3)


เพื่อทำเช่นนั้น คุณต้องขอเกี่ยว นอกจากนี้ คุณต้องตั้งค่า ID สำหรับแบบฟอร์ม เช่น addItemForm

//Anywhere in your client code
Autoform.hooks({
  addItemForm : {
    onSubmit : function(doc) {
      doc.groupId = /*Get the group id*/;
      this.done(); //We've finished
      return true; //Let autoForm do his default job now
    }
  }
});
person Kyll    schedule 17.12.2014
comment
เย็น. ฉันจะรับรหัสกลุ่มได้อย่างไร ตอนนี้ฉันได้รับ ID ที่มี this.params._id อยู่ข้างใน Router.route('/group/:_id', {}); - person Jamgreen; 17.12.2014
comment
หาก groupId ของคุณไม่ซ้ำกัน คุณสามารถส่งผ่านเป็นตัวแปรร่วมได้ อีกทางเลือกหนึ่งคือการตั้งค่าบนเซิร์ฟเวอร์... หรือเพื่อให้การควบคุมดีขึ้น คุณสามารถใช้วิธีการกับแบบฟอร์มของคุณได้ ดูประเภทที่นี่ - person Kyll; 17.12.2014

ฉันคิดว่าวิธีแก้ไขปัญหาหนึ่งไม่ใช่การแสดงตัวเลือกนี้ให้ผู้ใช้เห็น คุณต้องเพิ่ม optional:true ในฟิลด์ด้วย ดังนั้นจะยังคงใช้ได้เมื่อคุณส่งแบบฟอร์ม

จากนั้นเมื่อใช้ hooks คุณจะสามารถเพิ่มข้อมูลอื่นๆ ที่คุณต้องการได้

เอกสารพร้อมใช้งานแล้ว hooks on autoform

ฉันมักจะแก้ไขเอกสารใน before insert

AutoForm.hooks({
  myFormId: {
    before: {
      insert: function(doc, template) {
        //modify the document here
      }
    }
})
person Nicolas Grenié    schedule 17.12.2014

คุณสามารถใช้ doc=this หากบริบทข้อมูลของเทมเพลตพร้อมใช้งาน

ตัวอย่างเช่น:

<template name="itemForm">
  {{#autoForm id="insert-item-form" type="insert" collection=Collections.Items doc=this}}
    {{> afQuickField name="name"}}
    <div class="form-group">
      <button type="submit" class="btn btn-primary">Add item</button>
      <button type="reset" class="btn btn-default">Reset Form</button>
    </div>
  {{/autoForm}}
</template>

นอกจากนี้ คุณสามารถตั้งค่า hook ซึ่งจะถูกทริกเกอร์ก่อนการดำเนินการแทรก:

var itemsHooks = {
    before: {
        insert: function (doc) {
            doc.groupId = this.currentDoc._id;
            return doc;
        }
    }
};

AutoForm.addHooks('insert-item-form', itemsHooks);
person Matthias A. Eckhart    schedule 08.10.2015