2016-01-13 46 views
2

我想使用autoform將新文檔插入到db中。 Autoform鉤子在服務器上調用流星方法來插入文檔。通過autoform鉤子插入新文檔調用流星方法

我有這個模板...

{{#autoForm collection="Reports" id="addReport" type="insert"}} 
    <div class="row"> 
     <div class="col s6"> 
      {{> afQuickField name='hours'}} 
     </div> 
    </div> 
    <button class="btn waves-effect waves-light modal-action modal-close"><i class="material-icons">save</i></button> 
{{/autoForm}} 

則...

AutoForm.hooks({ 
    addReport: { 
     onSubmit: function(insertDoc) { 
      Meteor.call('addReport', insertDoc, function(error, result) { 
       if (error) alert(error.reason); 
      }); 
      return false; 
     } 
    } 
}); 

然後在服務器上的方法...

Meteor.methods({ 
    addReport: function(insertDoc) { 
     var report = _.extend(insertDoc, { 
      userId: Meteor.userId(), 
     }); 
     return Reports.insert(report); 
    } 
}); 

我有一個createdAtupdatedAt集合中的字段,但它們都具有autoValue,因此我相信不需要插入fr om客戶端或流星法。

所以收集與架構是這樣的:

Reports = new Meteor.Collection('reports'); 

Reports.attachSchema(new SimpleSchema({ 
    hours: { 
     type: Number, 
     label: "Number of hours", 
     decimal: true 
    }, 
    createdAt: { 
     type: Date, 
     label: "Created Date", 
     autoValue: function() { 
      if (this.isInsert) { 
       return new Date; 
      } else { 
       this.unset(); 
      } 
     }, 
     denyUpdate: true 
    }, 
    updatedAt: { 
     type: Date, 
     autoValue: function() { 
      if (this.isUpdate) { 
       return new Date() 
      } 
     }, 
     denyInsert: true, 
     optional: true 
    }, 
    "userId": { 
     type: String, 
     autoform: { 
      type: "hidden", 
     } 
    }, 
})); 

當我運行流星,形式顯示,而提交什麼都不做。沒有視覺提示如果有任何錯誤。在客戶端和服務器控制檯中都沒有錯誤消息。

我在做什麼錯誤或失蹤?

+0

這是模式嗎?它是否在模態之外工作? –

+0

@AutumnLeonard nope。它不在一個模式。 – Rexford

+0

嗯。我會一路添加一些控制檯日誌以查看正確調用的內容。 –

回答

2

aldeed /流星自動窗體文件:

// Called when form does not have a `type` attribute 
    onSubmit: function(insertDoc, updateDoc, currentDoc) { 
    Meteor.call()... 
    } 

我下面discovermeteor的書,我想用這本書的一些方法,但使用流星自動窗體包。

post_submit.html

<template name="postSubmit"> 

    {{#autoForm collection="Posts" id="insertPost"}} <-- no type 

      <div class="form-group"> 
       <div class="controls"> 
        {{> afQuickField name='title' class='title form-control'}} 
       </div> 
      </div> 
      <div class="form-group"> 
       <div class="controls"> 
        {{> afQuickField name='description' class='description form-control'}} 
       </div> 
      </div> 

      <input id="send" type="submit" value="Send" class="btn btn-primary"/> 

    {{/autoForm}} 

</template> 

post_submit.js

var postSubmitHook = { 

    onSubmit: function(insertDoc){ 
     Meteor.call('postInsert', insertDoc, function(error, result) { 

      if (error){ 
       Bert.alert(error.reason, 'danger', 'growl-top-right'); 
       $('#send').removeAttr('disabled'); 
       return; 
      } 

      Router.go('postPage', {_id: result._id}); 
     }); 
     return false; 
    } 
}; 

AutoForm.addHooks('insertPost', postSubmitHook); 
2

由於@Cristo GQ這樣做是正確的,我只是想確保答案是明確的足夠該線程的未來遊客


The onSubmithook會不惜一切


在另一方面使用爲autoForms type='normal'沒有任何type=before.insert僅僅是type='insert'而且沒有before.normal
這意味着當使用onSubmit掛鉤時,我們必須做任何「上班前」(如將當前用戶添加到文檔中)在onSubmit本身內。