首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >simple-schema/AutoForm集合中用户特定的字段插入[Meteor]

simple-schema/AutoForm集合中用户特定的字段插入[Meteor]
EN

Stack Overflow用户
提问于 2015-02-04 06:41:02
回答 3查看 827关注 0票数 2

我使用simple-schema和quickForm来允许用户将文档持久化到我的集合中,但是我想添加Meteor.userId()Meteor.user().username字段,最终只返回特定于当前用户的数据。因为quickForms没有显示它的insert()方法,所以我不确定在哪里添加这些字段。

下面是我用来允许用户将Speakers持久化到集合的模式。

代码语言:javascript
复制
    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
            }
    }));
EN

回答 3

Stack Overflow用户

发布于 2015-02-04 07:27:44

我不确定我是否正确理解了你的意思,但为什么不呢

代码语言:javascript
复制
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字段,可以执行以下操作

代码语言:javascript
复制
{{> quickForm collection="Speakers" id="speakerForm" type="insert" omitFields="userId"}}
票数 1
EN

Stack Overflow用户

发布于 2015-02-04 17:23:04

如果您希望省略输入表单中的一些字段,而自己使用值填充这些字段,则可以使用带有before钩子的afQuickField。

首先,将userId和username字段添加到您的模式中。

代码语言:javascript
复制
{{#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

代码语言:javascript
复制
Template.registerHelper("speakerCollection", Speakers);

然后添加一个before钩子来添加userId:

代码语言:javascript
复制
AutoForm.hooks({
  speakerForm: {
    before: {
      "insert": function (doc) {
        doc.userId = Meteor.userId();
        return doc;
      },
    }
});
票数 1
EN

Stack Overflow用户

发布于 2016-01-08 20:19:37

您可以使用autoValue定义自动插入的值:

代码语言:javascript
复制
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

代码语言:javascript
复制
userId: {
    type: String,
    autoValue: function() {
        return Meteor.user()._id;
    },

    // omit the userId field in autoform forms
    autoform: {
        omit: true
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/28310145

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档