简体   繁体   English

在不使用集合的情况下将服务器端计算返回给客户端

[英]Return server-side calculation to client without using a collection

Data is on the client generated and saved in a local collection: 数据是在客户端上生成的,并保存在本地集合中:

ListLocal = new Mongo.Collection(null);

let seed = [{
        name: "A",
        value: 1
    }, {
        name: "B",
        value: 2
    }, {
        name: "C",
        value: 3
    }];

    if (ListLocal.find().count() === 0) {
        ListLocal.forEach(function(entry) {
            ListLocal.insert(entry);
        });
    };

Then it is passed on the client to the server for further processing: 然后将其在客户端上传递给服务器以进行进一步处理:

Template.home.events({
    "click .btn-process": function(event) {
        event.preventDefault();
        let localData = ListLocal.find({}).fetch();

        Meteor.call("processData", localData);
    }
});  

The server accepts the data and passes it to a server-side Method that does some stuff with it, eg. 服务器接受数据并将其传递给服务器端方法,该方法对数据进行一些处理。 doubles all values of the passed Objects: 将传递的对象的所有值加倍:

Meteor.methods({
    processData(localData) {
        calculateDouble(localData);
    }
});

calculateDouble(localData) {
    // ...
    return calculatedData;
}

Now i want to display this calculatedData client-side without saving it in a server-side database. 现在,我想在客户端显示此calculatedData,而不将其保存在服务器端数据库中。

Question: How can i pass this data to the client? 问题:如何将这些数据传递给客户端?

Simply return the data from the method: 只需从方法中返回数据:

Meteor.methods({
    "processData": function(localData) {
        return calculateDouble(localData);
    }
});

calculateDouble(localData) {
    // ...
    return calculatedData;
}

To call the method in the event: 在事件中调用方法:

Template.home.events({
    "click .btn-process": function(event) {
        event.preventDefault();
        let localData = ListLocal.find({}).fetch();

        Meteor.call("processData", localData, function(error, result){
           //handle what you need to do with the result here.
        });
    }
});  

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 是否可以在不使用客户端Javascript的情况下使用服务器端Javascript网站? - Is a Server-side Javascript website possible without using client-side Javascript? 将 Apollo Client 与 NextJS 一起使用时服务器端渲染数据? - Server-side render data when using Apollo Client with NextJS? 客户端路由(使用react-router)和服务器端路由 - Client Routing (using react-router) and Server-Side Routing 客户端或服务器端处理? - Client-side or server-side processing? 服务器端和客户端JavaScript - Server-side and client-side JavaScript 客户端还是服务器端框架? - Client-side or server-side framework? 在没有任何服务器端语言的情况下在Javascript中使用MVC? - Using MVC in Javascript without any server-side language? javascript客户端向asp.net服务器端返回值 - javascript client-side return value to asp.net server-side 在客户端使用Jade语言,应在服务器端进行编译而不影响条件 - Use Jade Language in Client Side which should be compiled in server-side without affecting conditionals 在客户端裁剪和上传图像而不涉及服务器端代码 - Crop and upload image on client-side without server-side code involve
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM