[英]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.