【发布时间】:2015-10-08 01:50:18
【问题描述】:
我很困惑,因为在我的情况下,我并不真正了解我应该如何构建我的整个项目文件夹以及何时调用某种方法来在集合(数据库)中插入记录。
这就是我正在做的事情。每次当“雇主”在我的网站上注册时,我都想在名为 Employer = new Meteor.Collection("employer"); 的 Mongo 集合中为他创建一条记录@
这是我想象的:
文件夹结构:
client/client.js
collections/collections.js
server/server.js
server/methods.js
COLECTIONS.js
Employer = new Meteor.Collection("employer");
SERVER.JS
Meteor.startup(function(){
Accounts.onCreateUser(function (options, user) {
if (options.profile.type == 1) {
Roles.setRolesOnUserObj(user, ['employer']);
user.profile = options.profile
Meteor.publish("employer", function(){ //actually, here is where I want to write the newly created user into the Employer collections (as I have this selection through TYPE parameter)
return Employer.find();
});
}
else{
Roles.setRolesOnUserObj(user, ['employee']);
user.profile = options.profile
}
return user;
});
方法.js
Meteor.methods({
addEmployer: function () {
Employer.insert({
createdAt: new Date(),
owner: Meteor.userId(),
});
}
});
CLIENT.js
if (Meteor.isClient) {
// This code only runs on the client
// (client-side)
Template.Homepage.created = function() {
if (Accounts._verifyEmailToken) {
Accounts.verifyEmail(Accounts._verifyEmailToken, function(err) {
if (err != null) {
if (err.message = 'Verify email link expired [403]') {
console.log('Sorry this verification link has expired.')
}
} else {
console.log('Thank you! Your email address has been confirmed.')
Meteor.call('addEmployer'); // when he actually verifies hes email, he is added to the Employer colelction, but this is a bit tricky as I don't know if the person verifying the email is EMPLOYEE OR EMPLOYER?
}
});
}
};
}
现在我的问题是:
- “你认为这是实现逻辑的好方法吗? 注册雇主并将其添加到集合中”?
- “collections.js 是否应该只定义集合而没有额外的编程逻辑?”
- “最后,这是一种好的“设计模式”方法吗?”
编辑:当我想到它时,这里实际上不需要延迟补偿,所以我只会在用户注册并保存在用户集合中之后,将我的新雇主添加到服务器上的集合 EMPLOYER 中?
【问题讨论】:
-
您的代码目前似乎可以运行,并且您正在寻求改进它。一般来说,这些问题对于本网站来说过于固执己见,但您可能会在CodeReview.SE 找到更好的运气。记得阅读their requirements,因为他们比这个网站更严格。