Meteor.js + autoform一个工作联系表单示例

时间:2015-02-02 16:32:16

标签: meteor

我们可以获得Meteor初学者的完整联系表单示例吗?

到目前为止的步骤。

  • 创建架构
  • 在模板中显示联系表单
  • 确保使用方法传递值
  • 添加所需的包
  • 为客户端
  • 创建帮助程序
  • 为服务器端创建方法(发送电子邮件)
  • 显示成功消息

使用https://github.com/aldeed/meteor-autoform#an-example-contact-form

上的信息

1 个答案:

答案 0 :(得分:5)

enter image description here

<强>两者/集合/ contact.coffee

@Contacts = new Meteor.Collection('contacts')

Schemas.Contacts = new SimpleSchema
  name:
    type: String
    label: "Your name"
    max: 50
    optional: true
    autoform:
      placeholder: "John Doe"

  email:
    type: String
    regEx: SimpleSchema.RegEx.Email
    label: "E-mail address"
    optional: true
    autoform:
      placeholder: "John@doe.com"

  message:
    type: String
    label: "Message"
    max: 1000
    optional: true
    autoform:
      placeholder: "Message"
      rows: 3

Contacts.attachSchema(Schemas.Contacts)

<强>视图/接触/ contact.html

<template name="contactPage">
  <h2>Get in Contact</h2>
  {{> quickForm
    schema=contactFormSchema
    id="contactForm"
    type="method"
    meteormethod="sendEmail"
    template="bootstrap3-horizontal"
    label-class="col-sm-3"
    input-col-class="col-sm-9"
  }}
</template>

<强> .meteor /包

coffeescript
aldeed:collection2
aldeed:simple-schema
aldeed:autoform
twbs:bootstrap
email

<强>视图/接触/ contact.coffee

if Meteor.isClient
  Template.contactPage.helpers
    contactFormSchema: ->
      Schemas.Contacts

服务器/ contact-send.coffee

if Meteor.isServer
  Meteor.methods
    sendEmail: (doc) ->
    # Important server-side check for security and data integrity
    check doc, Schemas.contacts
    # Build the e-mail text
    text = 'Name: ' + doc.name + '\n\n' + 'Email: ' + doc.email + '\n\n\n\n' + doc.message
    @unblock()
    console.log "about to send the email"
    # Send the e-mail
    Email.send
      to: 'someone@somewhere.com'
      from: doc.email
      subject: 'Website Contact Form - Message From ' + doc.name
      text: text