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