基本流星异步概念:提交电子邮件和发送电子邮件

时间:2015-02-19 19:54:19

标签: javascript node.js mongodb meteor

我对Meteor框架非常陌生,并且很难掌握其概念,从传统的响应和请求背景(NodeJS异步回调样式)到Meteor。

我只是想建立一个登陆页面,告诉我你的姓名和电子邮件地址。用户单击“提交”后,我希望将用户保存在数据库(MongoDB)中,并向Mandrill端点发送请求,以便Mandrill可以向该特定电子邮件地址发送电子邮件。

我在传统回应和请求中的方法将是这样的。用户提交他们的姓名和电子邮件地址,这将是对我的服务器的POST请求,在我的服务器上,我的ORM会将其保存到数据库中,并且在回调成功后,我向Mandrill发送了一封要求发送电子邮件的请求。这个特定的用户。

我在Meteor中的代码如下:

Template.welcome.events({
  'submit form': function(e) {
    e.preventDefault();

    var subscribe = {
      name: $(e.target).find('[name="name"]').val(),
      email: $(e.target).find('[name="email"]').val()
    };

    var errors = validateSubscribes(subscribe);
    if (Object.keys(errors).length > 0) {
      for (var type in errors) {
        toast(errors[type], 2000);
      }
      return;
    }

    Subscribes.insert(subscribe, function(error, result) {
      if (error)
        return toast("Oops, something is wrong, try again");

      if (result) {
        $(e.target).find('button:submit')
                   .attr("disabled", "disabled");
        $(e.target).find('[name="name"]')
                   .val("")
                   .attr("disabled", "disabled");
        $(e.target).find('[name="email"]')
                   .val("")
                   .attr("disabled", "disabled");

        return toast('Thank you for subscribing!', 3000);
      }
    });
  }
});

我的Mandrill代码如下(从https://atmospherejs.com/wylio/mandrill获得)

#server code
Meteor.Mandrill.sendTemplate({
    "key": "YOUR_MANDRILL_API_KEY", // optional, if you set it in with Meteor.Mandril.config() already
    "template_name": "YOUR_TEMPLATE_SLUG_NAME",
    "template_content": [
      {}
    ],
    "message": {
        "global_merge_vars": [
            {
                "name": "var1",
                "content": "Global Value 1"
            }
        ],
        "merge_vars": [
            {
                "rcpt": "email@example.com",
                "vars": [
                    {
                        "name": "fname",
                        "content": "John"
                    },
                    {
                        "name": "lname",
                        "content": "Smith"
                    }
                ]
            }
        ],
        "to": [
        {"email": email@example.com}
        ]
    }
});

另外,在收藏中,我允许订阅

Subscribes = new Mongo.Collection('subscribes');

Subscribes.allow({
  insert: function(userId, subscribe) { return true; }
});

validateSubscribes = function(subscribe) {
  var errors = {},
      regExp = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;

   if (!subscribe.name)
    errors.name = 'Please fill in a name';

  if (!subscribe.email || !regExp.test(subscribe.email))
    errors.email = 'Please fill in a valid email';

  return errors;
};

我有三个问题:

  • 为什么要使用allow?为什么不使用Meteor.method和Meteor.call?
  • 我在哪里放置mandrill代码?在回调中?我认为Meteor的编程风格应该是同步风格吗?
  • 我可以将所有验证逻辑放在这里,并且我在此提交表单中的所有jQuery UI逻辑单独单击。我可以看到这很快变得臃肿。有没有具体的方法来组织所有这些?

非常感谢您的回答。流星非常酷,只需稍微包裹一下。

基督教

1 个答案:

答案 0 :(得分:1)

  1. 我们中的许多人专门使用Meteor.Method。请参阅Discover Meteor博客,获取良好的起点。 https://www.discovermeteor.com/blog/meteor-methods-client-side-operations/ 就我而言,我插入的几乎每个文档都有一个日期戳&amp; userId附加,所以我必须使用方法,否则用户可以在客户端声明任意Id。即使它不是必要的,它只是一种更简单的思维方式,而不是在脑海中运行允许/拒绝逻辑(记住,1是真的,你已经搞砸了)。这就是为什么有些人建议只使用deny或安装另一个包......

  2. 服务器上的Meteor使用光纤,这使代码LOOK同步。 https://www.eventedmind.com/feed/nodejs-using-futures 我将insert移到Meteor.method来整理此操作。然后,我用您从客户端收集的文件调用mandrill&amp;在服务器上验证。 (IIRC Mandrill需要一个API密钥,因此您可能不希望在客户端代码中出现这种情况,无论如何)

  3. 查看collection2和简单架构。它避免了许多凌乱的服务器端检查,选择和清理。学习曲线有点高和还有其他固体包装,但这是非正式的流星事实标准。