流星将数据从客户端传递到服务器 [英] Meteor pass data from client to server

查看:78
本文介绍了流星将数据从客户端传递到服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个注册表,当用户单击提交"按钮时,每个文本框中的值都将发送到服务器以插入该数据,并返回true/false.

I have a registration form, and when the user clicks the submit button the value in every textbox will be sent to server to insert that data, and return true/false.

客户:

Template.cust_register.events({
    'click button': function(){
          var email = $('#tbxCustEmail').val();
          var msg = $('#tbxCustMsg').val();
          var isSuccess = insertMsg(email,msg);
          if(isSuccess){
             alert("Success");
          }else alert("Try again");
    }
});

服务器:

function insertMsg(email,msg){
     Messages.insert({Email:email,Message:msg});
     return true;
}

这证明是行不通的. 怎么解决呢? 许多人说使用发布/订阅",但我不知道该如何使用.

This turned out to not work. How to solve this? Many people said "use publish/subscribe", but I don't understand how to use that.

推荐答案

首先,观看入门级截屏视频并阅读文档的数据和安全部分.

First, watch the introductory screencast and read the Data and security section of the docs.

您在发布/订阅模型中的代码如下:

Your code in a publish/subscribe model would look like this:

常用:

Messages = new Meteor.Collection('messages');

客户:

Meteor.subscribe("messages");

Template.cust_register.events({
    'click button': function(){
          var email = $('#tbxCustEmail').val();
          var msg = $('#tbxCustMsg').val();
          Messages.insert({Email:email,Message:msg});
    }
});

服务器:

Meteor.publish("messages", function() {
    return Messages.find();
});

这篇关于流星将数据从客户端传递到服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆