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

标签 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";
  }
})

谢谢

最佳答案

目前您无法直接向所有客户端广播方法调用。至少据我所知。但解决方法是创建一个名为 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/

相关文章:

javascript - 在浏览器控制台上为未经身份验证的用户禁用 Meteor Router.routes

javascript - 使用 Enzyme 进行 Meteor/React Mocha 测试 - 找不到模块 'has'

javascript - Meteor, $ 在客户端定义但不在服务器上定义

javascript - meteor 内容不显示

meteor - 如何为我的 Meteor 应用程序添加 oplog 支持?

meteor - 如何在 Meteor.http.call 中包含用户代理信息? MediaWiki 需要它

node.js - 如何解决未捕获的 ReferenceError : COLLECTION is not defined at <anonymous>:1:1 in Meteor. js/MongoDB

javascript - MongoDB:获取文档并且只有一个数组对象

javascript - 为什么此代码似乎没有将记录添加到 Meteor.users.profile 对象?

meteor - 如何避免cursor.observe 上的竞争条件?