我使用simple-schema和quickForm来允许用户将文档持久化到我的集合中,但是我想添加Meteor.userId()和Meteor.user().username字段,最终只返回特定于当前用户的数据。因为quickForms没有显示它的insert()方法,所以我不确定在哪里添加这些字段。
下面是我用来允许用户将Speakers持久化到集合的模式。
Speakers = new Meteor.Collection('speakers');
Speakers.attachSchema(new SimpleSchema({
first: {
type: String,
label: "first name",
max: 200
},
last: {
type: String,
label: "last name",
max: 200
},
date: {
type: Date,
label: 'date',
optional: true
}
}));发布于 2015-02-04 07:27:44
我不确定我是否正确理解了你的意思,但为什么不呢
Speakers.attachSchema(new SimpleSchema({
first: {
type: String,
label: "first name",
max: 200
},
last: {
type: String,
label: "last name",
max: 200
},
date: {
type: Date,
label: 'date',
optional: true
},
userId: {
type: String
}
}));如果要根据插入记录的用户自动填充,请使用autovalue
要使用quickForm而不显示userId字段,可以执行以下操作
{{> quickForm collection="Speakers" id="speakerForm" type="insert" omitFields="userId"}}发布于 2015-02-04 17:23:04
如果您希望省略输入表单中的一些字段,而自己使用值填充这些字段,则可以使用带有before钩子的afQuickField。
首先,将userId和username字段添加到您的模式中。
{{#autoForm collection=speakerCollection id="speakerForm" type="insert"}}
{{> afQuickField name="first"}}
{{> afQuickField name="last"}}
{{> afQuickField name="date"}}
<div>
<button type="submit" class="btn btn-primary">Submit</button>
<button type="reset" class="btn btn-default">Reset</button>
</div>
{{/autoForm}}您需要添加一个帮助器来返回speakerCollection
Template.registerHelper("speakerCollection", Speakers);然后添加一个before钩子来添加userId:
AutoForm.hooks({
speakerForm: {
before: {
"insert": function (doc) {
doc.userId = Meteor.userId();
return doc;
},
}
});发布于 2016-01-08 20:19:37
您可以使用autoValue定义自动插入的值:
Speakers = new Meteor.Collection('speakers');
Speakers.attachSchema(new SimpleSchema({
userId: {
type: String,
autoValue: function() {
return Meteor.user()._id;
}
},
author: {
type: String,
autoValue: function() {
return Meteor.user().username;
}
},
}));如果您想省略自动表单中的某些字段,您可以设置autoform.omit = true
userId: {
type: String,
autoValue: function() {
return Meteor.user()._id;
},
// omit the userId field in autoform forms
autoform: {
omit: true
}
}https://stackoverflow.com/questions/28310145
复制相似问题