基本meteorasynchronous概念:提交电子邮件和发送电子邮件

对于meteor框架来说,我真的很陌生,并且在传统的响应和请求背景(以NodeJSasynchronouscallback方式)到Meteor方面有一些难以理解的概念。

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

我在传统回应和要求中的做法就是这样。 用户提交他们的姓名和电子邮件地址,这将是一个POST请求到我的服务器,并在我的服务器上,我的ORM将它保存到数据库,callback成功后,我打了一个请求,以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; }; 

我有三个问题:

  • 为什么使用允许? 为什么不使用Meteor.method和Meteor.call?
  • 我在哪里把mandrill代码? 在callback里面? 我以为Meteor的编程风格应该是同步风格?
  • 我可以把我所有的validation逻辑放在这里,我的所有jQuery UI逻辑都在这个提交表单事件中单击。 我可以看到这很快就臃肿起来。 有没有组织所有这些的具体方式?

非常感谢你的回答。 meteor是超级酷,只需要把我的头围绕一点。

基督教

  1. 我们中的许多人完全使用Meteor.Method 。 查看Discover Meteor博客是一个很好的起点。 https://www.discovermeteor.com/blog/meteor-methods-client-side-operations/在我的情况下,我插入的几乎每个文档都附有一个datestamp&userId,所以我必须使用方法,否则用户可以声明客户端上的任意Id&#x3002; 即使没有必要,它只是一种简单的思维方式,而不是通过头脑中的允许/拒绝逻辑来运行(记住,1是真实的,你被搞砸了)。 这就是为什么有些人推荐只使用拒绝,或安装另一个软件包…

  2. 服务器上的Meteor使用光纤,这使得代码LOOK同步。 https://www.eventedmind.com/feed/nodejs-using-futures我将通过移动您&#x7684;insertMeteor.method组织这Meteor.method 。 然后,我会用从客户端收集的文档和在服务器上validation的文档调用mandrill。 (IIRC Mandrill需要一个APIkey,所以你可能不希望在客户端代码中挂起)

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