作为我的应用程序开发环境的一部分,我创建了一些种子数据,这些数据可以创建用户并将一些数据添加到他们的个人资料字段中。
我现在可以在我的应用程序的配置文件视图中显示该数据。
我的问题是允许用户在注册时添加这些数据的最佳方式是什么?
我已经尝试使用表单作为“配置文件”菜单集的一部分来执行此操作,但我没有成功更新该字段的数据。未添加新的/更新的数据。
我的模板看起来像这样
<template name="profileForm">
<form class="profileform form-horizontal" role="form">
<div class="form-group">
<!-- <label for="inputFirstName">First Name</label> -->
<div class="col-sm-4">
<input id="firstname" type="text" name="firstname" placeholder={{firstName}} />
</div>
</div>
<div class="form-group">
<!-- <label for="inputFirstName">Last Name</label> -->
<div class="col-sm-4">
<input id="lastname" type="text" name="lastname" placeholder={{lastName}} />
</div>
</div>
<div class="form-group">
<!-- <label for="inputFirstName">E-Mail Address</label> -->
<div class="col-sm-4">
<input id="email" type="email" name="email" placeholder={{email}} />
</div>
</div>
<div class="form-group">
<!-- <label for="inputFirstName">Phone Number</label> -->
<div class="col-sm-4">
<input id="phone" type="tel" name="phone" placeholder={{phoneNumber}} />
</div>
</div>
<div class="form-group">
<!-- <label for="inputFirstName">School</label> -->
<div class="col-sm-4">
<input id="school" type="text" name="school" placeholder={{schoolName}} />
</div>
</div>
<div class="form-group">
<!-- <label for="inputFirstName">First year</label> -->
<div class="col-sm-4">
<input id="firstschoolyear" type="text" name="firstschoolyear" placeholder={{firstSchoolYear}} />
</div>
</div>
<div class="form-group">
<!-- <label for="inputFirstName">Last Year</label> -->
<div class="col-sm-4">
<input id="lastschoolyear" type="text" name="lastschoolyear" placeholder={{lastSchoolYear}} />
</div>
</div>
<div class="form-group">
<div class="col-sm-4">
<label>Did you Matriculate Here?</label>
</div>
<div class="col-xs-3">
<select class="form-control">
<option>Yes</option>
<option>No</option>
</select>
</div>
</div>
<div class="form-group">
<!-- <label for="inputFirstName">House Name</label> -->
<div class="col-sm-4">
<input id="housename" type="text" name="housename" placeholder={{houseName}} />
</div>
</div>
<div class="form-group">
<!-- <label for="inputFirstName">Country Living In</label> -->
<div class="col-sm-4">
<input id="country" type="text" name="country" placeholder={{country}} />
</div>
</div>
<div class="form-group">
<!-- <label for="inputFirstName">City</label> -->
<div class="col-sm-4">
<input id="cityofresidence" type="text" name="cityofresidence" placeholder={{cityOfResidence}} />
</div>
</div>
<div class="form-group">
<!-- <label for="inputFirstName">Employment Industry</label> -->
<div class="col-sm-4">
<input tid="emplindustry" ype="text" name="emplindustry" placeholder={{emplIndustry}} />
</div>
</div>
<button class="profilesubmit">Update Profile</button>
</form>
我的 JS 文件看起来像这样
Template.profileForm.events({
"submit .profileform": function (event) {
// This function is called when the profile form is submitted
var firstname = event.target.firstname.value,
lastname = event.target.astname.value,
email = event.target.email.value,
phone = event.target.phone.value,
school = event.target.school.value,
firstschoolyear = event.target.firstschoolyear.value,
lastschoolyear = event.target.lastschoolyear.value,
matriculated = event.target.matriculated.value,
housename = event.target.houseelname.value,
country = event.target.country.value,
cityofresidence = event.target.cityofresidence.value,
emplindustry = event.target.emplindustry.value;
if(Meteor.userId())
{
Meteor.users.update({_id: Meteor.userId()},{$set:{
"profile.firstname": firstname,
"profile.lastname": lastname,
"profile.phone": phone,
"profile.school": school,
"profile.firstschoolyear": firstschoolyear,
"profile.lastschoolyear": lastschoolyear,
"profile.matriculated": matriculated,
"profile.housename": housename,
"profile.country": country,
"profile.cityofresidence": cityofresidence,
"profile.emplindustry": emplindustry,
"profile.joined": new Date(), // current time
}});
}
// Clear form
//event.target.text.value = "";
// Prevent default form submit
return false;
}
});
Template.profileForm.helpers({
email: function() {return Meteor.user().emails[0].address},
userName: function () {return Meteor.user().username},
firstName: function () {return Meteor.user().profile.firstname},
lastName: function () {return Meteor.user().profile.lastname},
phoneNumber: function () {return Meteor.user().profile.phone},
schoolName: function () {return Meteor.user().profile.schoolName},
firstSchoolYear: function () {return Meteor.user().profile.firstschoolyear},
lastSchoolYear: function () {return Meteor.user().profile.lastschoolyear},
matriculated: function () {return Meteor.user().profile.matriculated},
houseName: function () {return Meteor.user().profile.housename},
country: function () {return Meteor.user().profile.country},
cityOfResidence: function () {return Meteor.user().profile.cityofresidence},
emplIndustry: function () {return Meteor.user().profile.emplindustry},
signedUp: function () {return Meteor.user().profile.joined},
});
我使用了 Allow 功能来允许用户编辑他们的 profiel 字段。
Meteor.publish("userData", function () {
return Meteor.users.find({_id: this.userId},{ fields: {'profile': 1}});
});
Meteor.users.allow({
update: function (userId, user, fields, modifier) {
// can only change your own documents
if(user._id === userId)
{
Meteor.users.update({_id: userId}, modifier);
return true;
}
else return false;
}
});
我的订阅看起来像这样。
Meteor.subscribe('userData');
我坚信我缺乏编程技能导致我遗漏了一些重要的东西。