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

孤人 提交于 2019-12-24 12:50:25

问题


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

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?


回答1:


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

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



回答2:


You could try the before hook approach:

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

In your template:

{{#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}}

Then your autoform.js

var addUserHook = {
  before: {
    insert: function(doc) {
      if(Meteor.userId()){
        doc.ownerId = Meteor.userId();
      }

      return doc;
    }
  }
}

AutoForm.addHooks('exerciseForm', addUserHook);

The above adds the ownerId on the fly just when about to save to collection.

As stated in the docs on 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.



来源:https://stackoverflow.com/questions/33381963/how-to-add-user-id-information-in-the-schema-and-also-hide-form-autoforms

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!