2012-04-23 19 views
8

我想知道在客戶端從Meteor方法調用中獲得結果後,如何調用js函數。我能夠得到的唯一的東西是僅在實際方法調用的客戶端上調用函數myFunc。 任何想法如何我可以調用所有當前訂閱的客戶端上的功能?從服務器獲取結果後在流星中調用客戶端js函數

這裏是代碼:

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

感謝

回答

11

目前,您不能播放一個方法調用直接所有客戶端。至少據我所知。但是解決方法是創建一個名爲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."}); 
+2

由於使用在客戶端上看到()函數的伎倆。 – Gavriguy 2012-04-25 06:10:03

+0

@greg,你在服務器代碼上拼錯流星。 – 2013-06-12 12:24:33

+0

@greg,哦,收藏,應該是Collection – 2013-06-12 12:33:35

2

另一種選擇是使用Meteor Stream package,目的是爲了避免在服務器端使用mongodb集合。它確實支持客戶端到客戶端,服務器到客戶端,客戶端到服務器和服務器到服務器的消息傳遞,包括對支持Meteor Cluster

如果你想留在流星只使用集合,下面的代碼可以讓你廣播消息從客戶端到所有客戶端或從服務器到所有訂閱客戶端的消息。只要使用這種機制,一旦接收到正確的消息,就會在客戶端觸發一個功能。代碼的製作方式使您永遠不會將無用的項目留在集合中。

Messages = new Meteor.Collection("messages"); 

if (Meteor.isClient) { 

    Meteor.subscribe("messages"); 

    var query = Messages.find({}); 
    var handle = query.observe({ 
     added: function(document) 
     { 
      console.log(document.message); 
     } 
    }); 

    // Test the mechanism from the client side 
    Meteor.call("client talked"); 
} 

if (Meteor.isServer) { 
    Meteor.startup(function() { 
     Messages.remove({}); 
    }); 

    Meteor.publish("messages", function() 
    { 
     // you might add an optional filter in order to broadcast only the messages you want to the client 
     return Messages.find(); 
    }); 

    function talk(message) 
    { 
        var id = Messages.insert({"message":message}); 
        Messages.remove(id); 
    } 

    Meteor.methods(
      { 
       talk: function(message) 
       { 
        // you might filter here if the clients can talk using this.userId 
        talk(message); 
       } 
      }); 

    // test the mechanism from the server side 
    talk("server talked"); 
} 
0

我簡單的調用JavaScript客戶端函數的方法是在您的集合綁定的html模板中添加一個腳本標記。任何時候插入一個新的項目,這個標籤將被插入到客戶端將運行你的功能。我有一個集合電話上傳與一些屬性,如名稱。下面的模板觸發drawpoints()在收到中上傳收集新項目的客戶端功能:

{{#each uploads}} 
     <tr> 
      <td>{{name}}</td> 
      <td> 
       <div class="alert alert-success"><a href="{{url download=true}}">Download Here</a></div> 
      </td> 
     </tr> 
     <script>drawpoints();</script> 
    {{/each}}