簡體   English   中英

帶有collection2的Meteor Autoform包不提交表單

[英]Meteor Autoform package with collection2 does not submit the form

我正在使用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鈎子以及Meteors內置模板'submit'事件處理程序。 如果我使用jQuery onsubmit事件處理程序,它會注冊該事件。 我不能為此目的使用jQuery,因為autoform必須驗證輸入。

由於createdmodified是必填字段,因此很可能無法提交,因為表單中缺少這些字段,這意味着表單無效。 實際上有幾種不同的方法可以解決這個問題:

  1. 讓它們成為可選的(可能不是你想要的)
  2. 使用不同的模式(沒有這兩個字段的schema )作為autoform的schema屬性。
  3. 添加一個“before method”鈎子,用於在提交的doc中設置這兩個字段。
  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;
        }
    }
}

另外,為了在開發過程中更容易找出這樣的問題,我建議啟用調試模式

您是否允許在您的收藏中插入/更新? 請參見http://docs.meteor.com/#dataandsecurity

我打賭如果你運行下面的兩個命令,它會工作。

meteor add insecure
meteor add autopublish 

現在嘗試提交。

如果它確實有效,請關閉自動發布和不安全狀態

meteor remove insecure
meteor remove autopublish

然后編寫你的允許方法,例如

Node.allow({
  insert: function (userId, nodeDoc) {
    // write some logic to allow the insert 
    return true;
  }
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM