Issues inserting parent ID into autoform with meteor

时间:2016-03-04 18:07:24

标签: meteor meteor-autoform

I am having issues with inserting a parent id in autoform with meteor. I am using the autoform hook:

Template.NewLecture.onRendered( function(){

    AutoForm.hooks({
        insertLectureForm: {
            insert: {
                method: function( doc ) {
                    doc.courseId = Template.parentData(1)._id;
                    return doc;
                }
            }
        }
    });
});

The Template.parentData(1)._id properly returns the correct ID but it doesn't seem to save into the document. This is the template:

    <template name="NewLecture">
        <div class="new-lecture-container">
            {{> quickForm collection='Lectures' id='insertLectureForm' type="insert" class="new-lecture-form"}}
        </div>
    </template>

This is the schema:

LectureSchema = new SimpleSchema({
    name: {
        type: String,
        label: "Lecture",
        optional: true
    },
    courseId: {
        type: String,
        label: "CourseId",
        autoform: {
            type: "hidden"
        }
        optional: true
    }
});

Lectures.attachSchema(LectureSchema);

What exactly am I missing here? The lecture inserts fine except the courseId is black. If I am using the autoform hooks, to set the courseId value, how should I be setting it in the schema to accept that? Thanks!

EDIT: I have not solved it, but the problem appears to be that Template.parentData(1)._id does not return the same thing inside the insert method as inside the onRender section. Using this new hook:

Template.NewLecture.onRendered( function(){
    console.log(Template.parentData(1))

    AutoForm.hooks({
        insertLectureForm: {
            before: {
                insert: function( doc ) {
                    doc.courseId = Template.parentData(1)._id;
                    console.log(Template.parentData(0))
                    console.log(Template.parentData(1))
                    console.log(Template.parentData(2))
                    console.log(Template.parentData(3))
                    console.log(doc)
                    return doc;
                }
            }
        }
    });
});

I get this output: enter image description here So clearly the first and third console.logs do not match here. How can I either pass in or retrieve the contents of the first console log within that hook?

2 个答案:

答案 0 :(得分:1)

 {{#autoForm collection="Lectures" id="someID"  class="default-form form-horizontal" type="inser"}}
  {{>afQuickField name='courseId' type="hidden" value=helper}}    
{{/autoForm}}

Template.NewLecture.helpers({
  helper:function(){
   return this._id
  }
})

答案 1 :(得分:0)

谢谢赛!因此,解决方案是您必须继续通过帮助程序传递您的父ID:

<template name="AdminLectureList">
    {{> NewLecture courseId = _id }}
</template>

<template name="NewLecture">
    <div class="new-lecture-container">
        {{> quickForm collection='Lectures' id='insertLectureForm' courseId=courseId type="insert" class="new-lecture-form"}}
    </div>
</template>

Template.NewLecture.onRendered( function(){
    AutoForm.hooks({
        insertLectureForm: {
            before: {
                insert: function( doc ) {
                    doc.courseId = Template.parentData(0).courseId;
                    return doc;
                }
            }
        }
    });
});