javascript - 带有 collection2 的 Meteor Autoform 包不提交表单

标签 javascript node.js meteor meteor-blaze

我正在将 collection2 和 autoform 与一级嵌套模式结合使用

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 Hook 以及 Meteors 内置模板“提交”事件处理程序。如果我使用 jQuery onsubmit 事件处理程序,它会注册该事件。我不能为此目的使用 jQuery,因为 autoform 必须验证输入。

最佳答案

由于 createdmodified 是必填字段,因此很可能不会提交,因为表单中缺少这些字段,这意味着表单无效。实际上有几种不同的方法可以解决这个问题:

  1. 将它们设为可选(可能不是您想要的)
  2. 为自动表单的 schema 属性使用不同的模式,一个没有这两个字段的模式。
  3. 添加一个“方法前” Hook ,在提交的文档中设置这两个字段。
  4. 使用 autoValue 为这两个字段生成值。

因为使用 autoValue 可以很容易地创建和修改日期,我会那样做。像这样:

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;
        }
    }
}

此外,为了帮助在开发过程中更轻松地找出此类问题,我建议启用 debug mode .

关于javascript - 带有 collection2 的 Meteor Autoform 包不提交表单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23156715/

相关文章:

javascript - Github 要点 : New Lines?

javascript - 设置cookie跨原点

javascript - 服务器终止时保存文件

javascript - Node.js 单元测试

javascript - Meteor - 如何在密码上使用服务器端验证

Meteor 应用程序 - 尝试运行时卡在 "Starting your app"

mongodb - Meteor:查询字段中是否存在字符串值并返回 bool 值响应

javascript - javascript 中 if 语句中的两个条件似乎无法正常工作

javascript - Angular 新手 : trying to use ng-change, 获取 "Module ' ngModel' 不可用”?

javascript - Node.JS 逐行读取字符串