我想看看在客户端从 Meteor 方法调用获得结果后如何调用 js 函数。我唯一能得到的是调用函数 myFunc
仅在进行实际方法调用的客户端上。
有什么想法可以在所有当前订阅的客户端上调用该函数吗?
这是代码:
function myFunc(error, result) {
alert(result);
}
if (Meteor.is_client) {
Template.container.events = {
'click input' : function () {
Meteor.call('someMethod',myFunc);
if (typeof console !== 'undefined')
console.log("You pressed the button");
}
};
}
if (Meteor.is_server) {
Meteor.startup(function () {
// code to run on server at startup
});
}
Meteor.methods({
someMethod: function() {
//console.log(!this.is_simulation);
return "something";
}
})
谢谢
最佳答案
目前您无法直接向所有客户端广播方法调用。至少据我所知。但解决方法是创建一个名为 Alerts 的集合并监视它的变化。然后,当您想向所有用户发送消息时,您可以更改警报中的文档:
客户:
Alerts = new Meteor.Collection("alerts")
Meteor.autosubscribe(function() {
Alerts.find().observe({
added: function(item){
alert(item.message);
}
});
});
服务器:
Alerts = new Meteor.Collection("alerts")
Meteor.publish("alerts", function(){
Alerts.find();
});
Alerts.remove({}); // remove all
Alerts.insert({message: "Some message to show on every client."});
关于meteor - 从服务器获取结果后在 Meteor 中调用客户端 js 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10274679/