本文介绍了带有collection2的Meteor Autoform包不提交表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用collection2和autoform与一个级别的嵌套模式

I am using using collection2 and autoform with one level nested schemas

Node = new Meteor.Collection('node',{
schema: new SimpleSchema({
       content: {
           type: String,
           label: "Content",
           max: 200
       }
       created: {
           type: Date,
           label: "Created Date"
       },
       modified: {
           type: Date,
           label: "Modified Date"
       }
   })
});

NodeMeta = new Meteor.Collection('Node_meta',{
schema: new SimpleSchema({
    desc:{
        type:String,
        max:200,
        label:"Description",
        optional:true
    }
})

});

Schema = {};

Schema.nodeWithMeta= new SimpleSchema({
  Node: {
     type: Node.simpleSchema()
  },
  Meta: {
     type: NodeMeta.simpleSchema()
  }
});


{{#autoForm schema=schema id="nodeForm" type="method" meteormethod="nodecreate"}}
  {{> afFieldInput name='Node.content' rows=8 }}
  {{> afFieldInput name='Meta.desc' rows=8}}    
  <button type="submit" class="btn btn-primary btn-submit">Create</button>
{{/autoForm}}

Template.nodeCreate.helpers({
  schema: function() {
    return Schema.nodeWithMeta;
  }
});

它不调用服务器方法。我尝试了autoform onSubmit钩子以及Meteors内置模板'submit'事件处理程序。如果我使用jQuery onsubmit事件处理程序,它会注册该事件。我不能为此目的使用jQuery,因为autoform必须验证输入。

It does not call server method. I tried autoform onSubmit hook as well as Meteors inbuilt templates 'submit' event handler. If I use jQuery onsubmit event handler, it registers the event. I cannot use jQuery for this purpose since autoform has to validate the inputs.

推荐答案

创建


  1. 让它们成为可选项(可能不是你想要的)

  2. 对于autoform的 schema 属性,使用不同的模式(没有这两个字段)。

  3. 添加一个before method钩子在提交的文档中设置这两个字段。

  4. 使用 autoValue 生成这两个字段的值。

  1. Make them optional (probably not what you want)
  2. Use a different schema, one without those two fields, for the schema attribute of the autoform.
  3. Add a "before method" hook that sets those two fields in the submitted doc.
  4. Use autoValue to generate the values for those two fields.

由于创建和修改日期很容易用,我会那样做。这样的事情:

Since created and modified dates are fairly easy to do with autoValue, I would do it that way. Something like this:

created: {
    type: Date,
    label: "Created Date",
    autoValue: function () {
        if (this.isInsert) {
          return new Date;
        } else {
          this.unset();
        }
    }
},
modified: {
    type: Date,
    label: "Modified Date",
    autoValue: function () {
        if (this.isInsert) {
          this.unset();
        } else {
          return new Date;
        }
    }
}

此外,为了帮助弄清楚在开发过程中,这样的问题更容易发生,我建议启用。

Also, to help figure out issues like this more easily while developing, I recommend enabling debug mode.

这篇关于带有collection2的Meteor Autoform包不提交表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-14 17:58