从服务器获取结果后,在Meteor中调用客户端js函数

Gav*_*guy 8 meteor

我试图看看在客户端从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";
  }
})
Run Code Online (Sandbox Code Playgroud)

谢谢

gre*_*reg 11

目前,您无法直接向所有客户端广播方法调用.至少据我所知.但是,解决方法是创建一个名为Alerts的集合并监视它以进行更改.然后,当您要向所有用户发送消息时,可以在警报中更改文档:

客户:

Alerts = new Meteor.Collection("alerts")

Meteor.autosubscribe(function() {
  Alerts.find().observe({
    added: function(item){ 
      alert(item.message);
    }
  });
});
Run Code Online (Sandbox Code Playgroud)

服务器:

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."});
Run Code Online (Sandbox Code Playgroud)

  • 谢谢使用客户端上的observe()函数做了伎俩. (2认同)