如何在模式中添加用户 ID 信息并隐藏表单自动表单?

How to add user id information in the schema and also hide form autoforms?

我正在学习 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 添加到架构中时,它显示在自动表单中,我不确定如何隐藏它,如果我隐藏它,那么我可以在自动表单中使用挂钩来添加值吗?

在架构中,您可以将 ownerId 定义为类型:"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
    },
  }
});

使用挂钩的替代方法是在您的自动表单中为您要在文档中设置的每个字段(包括 ownerId)使用 quickFields。使用此解决方案,您可以将 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();
    }
});

您可以尝试 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 上的文档所述:

For example, you might want to add the current user's ID to a document before it is inserted. You can use "before", "formToDoc", or "formToModifier" hooks to do this.