简体   繁体   中英

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.

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.
        });
    }
});  

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM