我想使用Meteor的autoform包为我的CAS_Entry
集合创建一个表单。该代码可以在下面看到。我还添加了已定义的钩子,不幸的是,仅执行了beginSubmit
和before
,没有任何条目添加到集合中。使用Meteor外壳,插入物就像一个饰物。
我很感谢任何提示。
addCasEntry.html,用于显示表单的模板:
{{#autoForm collection="CAS_Entry" type="insert" id="addCasEntryForm"}}
{{> afQuickField name="type" options="allowed"}}
{{> afQuickField name="description" rows="6" type="textarea"}}
{{> afQuickField name="file" type="cfs-file" collection="Images"}}
{{> afQuickField name="date" }}
<button type="submit" class="btn btn-primary">Add</button>
{{/autoForm}}
addCasEntry.js,添加调试钩子:
AutoForm.hooks({
addCasEntryForm: {
before: {
insert: function(doc) {
console.log(doc);
}
},
after: {
insert: function(error, result) {
console.log('Occured error: ' + error);
}
},
beginSubmit: function() {
console.log('begin submit');
},
onSuccess: function(formType, result) {
console.log("Insert succeeded");
console.log('Result ' + result);
},
onError: function(formType, error) {
console.log('Error!!!');
console.log(error);
}
}
});
SimpleSchema.debug = true;
/lib/collection/cas_entry.js:
CAS_Entry = new Mongo.Collection("cas_entries");
CAS_Entry.attachSchema(new SimpleSchema({
type: {
type: String,
allowedValues: ['reflection', 'evidence']
},
description: {
type: String,
optional: true
},
file: {
type: String,
optional: true,
},
timeUploaded: {
type: Date,
optional: true,
autoValue: function() {
return new Date();
}
},
date: {
type: Date,
}
}));
CAS_Entry.allow({
'insert': function() {
return true;
},
'update': function() {
return true;
}
});
这是控制台输出:
最佳答案
您不会提交表单,因为您没有将文档返回或传递到this.result();
挂钩内的before
。
AutoForm.hooks({
addCasEntryForm: {
// ...
before: {
insert: function(doc) {
console.log(doc);
return doc;
}
}
// ...
}
});
根据documentation,应根据定义的先决条件使用以下语句之一:
同步,提交:
return doc;
。同步,取消:
return false;
。异步,提交:
this.result(doc);
。异步,取消:
this.result(false);
。关于javascript - meteor AutoForm停止继续提交,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/35927512/