【问题标题】:How to add user id information in the schema and also hide form autoforms?如何在模式中添加用户 ID 信息并隐藏表单自动表单?
【发布时间】:2015-12-17 06:20:26
【问题描述】:

我正在学习 Meteor 的绳索,有点迷失在这里。我正在使用 collections2,autoform 来构建我的应用程序。我想将集合与用户 ID 信息一起存储。因此,当我们从服务器检索集合时,我只想显示用户创建的集合,而不是其他所有内容。这是架构。

    ExercisesSchema = new SimpleSchema({
    "name": {
        type: String,
        label: 'Name'
    },
    "workout.$.weight": {
        type: String,
        label: 'Weight'
    },
    "workout.$.reps": {
        type: String,
        label: 'Reps'
    },
    "notes": {
        type: String,
        label: 'Notes',
        optional: true
    }
});

在服务器端,我只想显示用户创建的锻炼

Meteor.publish('exercises', function () {
    return Exercises.find({owner: this.userId});
});

当我将用户 ID 添加到架构中时,它会显示在自动表单中,我不确定如何隐藏它,如果我隐藏它,那么我可以使用自动表单中的挂钩来添加值吗?

【问题讨论】:

    标签: meteor meteor-autoform simple-schema


    【解决方案1】:

    在架构中,您可以将 ownerId 定义为 type: "hidden"

    schema.js

    ExercisesSchema = new SimpleSchema({
        "name": {
            type: String,
            label: 'Name'
        },
        "ownerId": {
            type: String,
            autoform: {
                type: "hidden",
            }
        },
        "workout": {
            //not sure if you need this, you didn't have it in your
            type: [Object], 
            defaultValue: [] 
        },
        "workout.$.weight": {
            type: String,
            label: 'Weight'
        },
        "workout.$.reps": {
            type: String,
            label: 'Reps'
        },
        "notes": {
            type: String,
            label: 'Notes',
            optional: true
        }
    });
    

    按照你说的用钩子填充它。

    autoFormHooks.js

    AutoForm.hooks({
      exerciseForm: {
        formToDoc: function(doc) {
          doc.ownerId = Meteor.userId();
          return doc
        },
      }
    });
    

    使用钩子的替代方法是在 autoForm 内为您要在文档中设置的每个字段使用 quickFields,包括 ownerId。使用此解决方案,您可以将 ownerId 的 value 设置为 currentUser

    {{#autoForm collection="Exercises" id="exerciseForm" type="insert"}}
      <fieldset>
        <legend>Add an Exercise</legend>
        {{> afQuickField name='name'}}
        {{> afQuickField name='notes'}}
        {{> afQuickField name='ownerId' value=currentUserId}}
        {{> afQuickField name='workout'}}
      </fieldset>
      <button type="submit" class="btn btn-primary">Insert</button>
    {{/autoForm}}
    

    template.js

    Template.formTemplate.helpers({
        currentUserId: function () {
            return Meteor.userId();
        }
    });
    

    【讨论】:

    • 你有什么想法,是什么导致了以下错误,“错误:ownerId 字段的定义无效”。如果我删除 autoForm 定义,错误就会消失。将自动表单添加到架构中,错误又回来了。我已经安装了 simple-schema、collection2 和 autoform。
    • @challett 按照上述方法,我将两个对象同时保存到集合中。删除autoFormHooks.js 解决它
    【解决方案2】:

    你可以试试before钩子方法:

    ExercisesSchema = new SimpleSchema({
    ...
        "ownerId": {
            type: String,
            autoform: {
                type: "hidden",
            }
        },
    ...
    });
    

    在您的模板中:

    {{#autoForm collection="Exercises" id="exerciseForm" type="insert"}}
      <fieldset>
        <legend>Add an Exercise</legend>
        {{> afQuickField name='name'}}
        {{> afQuickField name='notes'}}
        <!-- Notice I've removed the ownerId from here. Will be added before saving to collection -->
        {{> afQuickField name='workout'}}
      </fieldset>
      <button type="submit" class="btn btn-primary">Insert</button>
    {{/autoForm}}
    

    然后你的autoform.js

    var addUserHook = {
      before: {
        insert: function(doc) {
          if(Meteor.userId()){
            doc.ownerId = Meteor.userId();
          }
    
          return doc;
        }
      }
    }
    
    AutoForm.addHooks('exerciseForm', addUserHook);
    

    上面的内容会在即将保存到集合时即时添加 ownerId。

    atmospherejs.com/aldeed/autoform 上的文档所述:

    例如,您可能希望将当前用户的 ID 添加到 插入之前的文档。您可以使用“之前”、“formToDoc”或 "formToModifier" 钩子来做这个。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-12-25
      • 1970-01-01
      • 2013-04-29
      • 2017-10-07
      • 2010-09-21
      • 1970-01-01
      • 2013-12-18
      • 1970-01-01
      相关资源
      最近更新 更多