2015-11-30 19 views
1

我想用autoform創建一個表單,當我點擊提交時,它會向集合中插入一個新行(數據行)。我發現了一個很好的例子,說明了我在這個頁面上想要什麼,但是我不能僅僅通過他們提供的解釋/代碼來​​使它工作。如何設置Meteor插入AutoForm以將數據插入到集合中?

http://autoform.meteor.com/insertaf

Schemas = {}; 

Template.registerHelper("Schemas", Schemas); 

Schemas.Person = new SimpleSchema({ 
    firstName: { 
    type: String, 
    index: 1, 
    unique: true 
    }, 
    lastName: { 
    type: String, 
    optional: true 
    }, 
    age: { 
    type: Number, 
    optional: true 
    } 
}); 

var Collections = {}; 

Template.registerHelper("Collections", Collections); 

People = Collections.People = new Mongo.Collection("People"); 
People.attachSchema(Schemas.Person); 

Meteor.publish(null, function() { 
    return People.find(); 
}); 

People.allow({ 
    insert: function() { 
    return true; 
    }, 
    remove: function() { 
    return true; 
    } 

{{#autoForm id="afInsertDemo" type="insert" collection=Collections.People}} 
    <div class="form-group {{#if afFieldIsInvalid name='firstName'}}has-error{{/if}}"> 
    <label class="control-label">{{afFieldLabelText name='firstName'}}</label> 
    {{> afFieldInput name='firstName'}} 
    {{#if afFieldIsInvalid name='firstName'}} 
    <span class="help-block">{{{afFieldMessage name='firstName'}}}</span> 
    {{/if}} 
    </div> 
    <div class="form-group {{#if afFieldIsInvalid name='lastName'}}has-error{{/if}}"> 
    <label class="control-label">{{afFieldLabelText name='lastName'}}</label> 
    {{> afFieldInput name='lastName'}} 
    {{#if afFieldIsInvalid name='lastName'}} 
    <span class="help-block">{{{afFieldMessage name='lastName'}}}</span> 
    {{/if}} 
    </div> 
    <div class="form-group {{#if afFieldIsInvalid name='age'}}has-error{{/if}}"> 
    <label class="control-label">{{afFieldLabelText name='age'}}</label> 
    {{> afFieldInput name='age'}} 
    {{#if afFieldIsInvalid name='age'}} 
    <span class="help-block">{{{afFieldMessage name='age'}}}</span> 
    {{/if}} 
    </div> 
    <div class="form-group"> 
    <button type="submit" class="btn btn-primary">Add Person</button> 
    <button type="reset" class="btn btn-default">Reset Form</button> 
    </div> 
{{/autoForm}} 

我收到以下錯誤信息:

模板沒有定義

+0

你可能運行['Template.registerHelper(姓名,職務)'](http://docs.meteor.com/#/full/template_registerhelper)而不是客戶端上。請嘗試將這些函數放入'/ client'目錄或使用['Meteor.isClient'](http://docs.meteor.com/#/full/meteor_isserver)。 –

回答

1

開始使用簡單的小號chema和autoform,而沒有對客戶端,服務器端或者兩者的定義進行深入瞭解可能是一個挑戰。就你而言,你需要在客戶端和服務器上都有模式和集合定義。服務器上的發佈和收集訪問規則以及客戶端上的模板。

我創建了以下meteorpad項目來說明(注意「/客戶端」,「/服務器」和「/共同」的前綴):

http://meteorpad.com/pad/Xue5BWFwcjyv4QvL2/AF%20Test

這個項目有一個合理的結構如下:

https://github.com/matteodem/meteor-boilerplate

+0

這是完美的!感謝您的解釋和鏈接。 –

+0

@LidoFernandez如果這樣做結束了,繼續並將其標記爲已接受,以便解決。 – DSK

相關問題