我想用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}}
我收到以下錯誤信息:
模板沒有定義
你可能運行['Template.registerHelper(姓名,職務)'](http://docs.meteor.com/#/full/template_registerhelper)而不是客戶端上。請嘗試將這些函數放入'/ client'目錄或使用['Meteor.isClient'](http://docs.meteor.com/#/full/meteor_isserver)。 –