8

クライアントが 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";
  }
})

ありがとう

4

4 に答える 4

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."});
于 2012-04-23T06:08:16.763 に答える
2

もう 1 つのオプションは、サーバー側で mongodb コレクションを使用しないようにすることを目的としたMeteor Stream パッケージを使用することです。Meteor Clusterのサポートを含む、クライアントからクライアントへ、サーバーからクライアントへ、クライアントからサーバーへ、およびサーバーからサーバーへのメッセージングをサポートします。

コレクションのみを使用して meteor を使用したい場合は、次のコードを使用して、クライアントからすべてのクライアントにメッセージをブロードキャストするか、サーバーからすべてのサブスクライブしたクライアントにメッセージをブロードキャストできます。このメカニズムを使用して、適切なメッセージを受信したら、クライアント側で関数を起動するだけです。コードは、無駄なアイテムがコレクションに残らないように作成されています。

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");
}
于 2014-04-06T06:13:13.877 に答える
0

JavaScript クライアント側関数を呼び出す簡単な方法は、コレクションにバインドされている HTML テンプレートに script タグを追加することです。新しいアイテムが挿入されるたびに、このタグがクライアントに挿入され、関数が実行されます。nameなどのいくつかのプロパティを持つコレクション呼び出しのアップロードがあります。次のテンプレートは、Uploads コレクションで新しいアイテムを受信すると、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}}
于 2015-08-03T23:19:05.097 に答える
0

Zeke の発言は気に入っていますが、Meteor 0.5.0 以降を使用している場合は、autosubscribe の代わりに Deps.autorun を使用してください... 詳細: https://groups.google.com/forum/#!topic/meteor-core/ mTa81RLvhbY および http://www.meteor.com/blog/2013/02/14/meteor-055-devshop-code-and-community-contributions

于 2013-08-10T01:33:39.847 に答える