如何在架构中添加用户ID信息并隐藏表单自动表单? [英] How to add user id information in the schema and also hide form autoforms?

查看:213
本文介绍了如何在架构中添加用户ID信息并隐藏表单自动表单?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am learning the ropes of Meteor and kind of lost here. I am using collections2, autoform for building my application. I want to store the collection along with user id information. So that when we retrieve the collection from the server, I want to show only the ones the user created not everything else. here is the schema.

    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
    }
});

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

on the server side I want to show only the workouts created by the user

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

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

When I added the user id to the schema, it shows up in the autoform, I am not sure how to hide it, if I hide it, then possibly I can use hooks in the autoform to add the values?

推荐答案

在架构中,您可以将ownerId定义为类型:隐藏"

In the schema you can define the ownerId as 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
    }
});

然后按照您所说的用钩子填充它.

And populate it with the hooks as you said.

autoFormHooks.js

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

使用钩子的另一种方法是对要在文档中设置的每个字段(包括ownerId)使用autoForm内的quickFields.使用此解决方案,您可以将ownerId的value设置为currentUser.

An alternative to using hooks would be to use a quickFields inside of your autoForm for each field that you want to set in the doc, including ownerId. With this solution you would set the value of ownerId to 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();
    }
});

这篇关于如何在架构中添加用户ID信息并隐藏表单自动表单?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆