从服务器获取结果后,调用Meteor中的客户端js函数 [英] Invoke a client js function in Meteor after getting results from the server
问题描述
我正在尝试查看客户端从Meteor方法调用获得结果后如何调用js函数.我唯一能得到的就是仅在进行实际方法调用的客户端上调用函数myFunc
.
有什么想法可以在所有当前订阅的客户端上调用该函数吗?
I'm trying to see how can I invoke a js function after the client gets a result from a Meteor method call. The only thing I was able to get is to invoke the function myFunc
only on the client that made the actual method call.
Any thoughts how i can invoke the function on all the currently subscribed clients?
下面是代码:
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的集合并监视它的更改.然后,当您要将消息发送给所有用户时,可以在警报"中更改文档:
Currently you can't broadcast a method call to all clients directly. At least as far as I can tell. But a work around would be to create a collection called Alerts and monitor it for changes. Then when you want to send a message to all your users you can change the document in 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中的客户端js函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!