流星 - 构建和处理集合

时间:2015-07-18 12:21:10

标签: mongodb meteor

我有一个困惑,因为在我的情况下,我并不真正理解我应该如何构建整个项目文件夹以及何时调用某个方法在集合(数据库)中插入记录。

这就是我正在做的事情。每当一个"雇主"在我的网站上注册我想在Mongo Collection中为他创建一个名为Employer = new Meteor.Collection("employer");

的记录

这就是我想象的:

文件夹结构:

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

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


}

现在我的问题是:

  1. "你认为这是实现逻辑的好方法吗? 在一个集合中注册并添加雇主"?
  2. " collections.js是否只定义了没有额外编程逻辑的集合?"
  3. "最后,这是一个很好的设计模式"方法"
  4. 编辑:当我考虑它时,实际上没有必要在这里进行延迟补偿,所以我会在用户注册并保存在USERS集合后立即将新的雇主添加到服务器上的EMPLOYER集合中?

1 个答案:

答案 0 :(得分:1)

我也在过去几个月研究流星。一开始就有很多混乱。经过一些研究和讨论,我得出了一些关于这个项目结构的结论。根据您的问题,这是一些演示示例......

文件夹结构:

both/ (OR lib/)          -- common code for server and client
  |- collections/        -- declare collections (e.g Employer = new Meteor.Collection("employer");)
  |- router     /        -- router code(e.g Router.route(..))

client/                  -- client side code
  |- global/             -- all global variable for client
  |- helpers/            -- global helper for client (for all templates)
  |- plugins/            -- all the plugins code(if you use any)
  |- stylesheets/        -- css / less files
  |- templates/          -- all templates
        |- home.html     -- home template(html)
        |- home.js       -- home template(js)

public/                  -- images/icons/fonts (meteor looking at this file)

server/                  -- server code
  |- methods/            -- server methods/API (e.g Meteor.methods({...}))
  |- publish/            -- publish code from server

这是我遵循的流星项目的基本文件夹结构。进一步referenceDocumentation。对于任何问题,请在评论中随意询问..

干杯。 :)