如何在Session变量上执行Meteor.call

时间:2021-10-26 16:35:25

Here is what I have:

这是我有的:

Templates:

<body>
  {{> resultSession}}
  {{> resultMethod}}
</body>

<template name="resultSession">
  <button>Click me</button>
  <p>Session.get('length') returned {{returned}}</p>
</template>

<template name="resultMethod">
  <p>Meteor.call returned {{returned}}</p>
</template>

Client-side:

Template.resultSession.events({
  'click button': function () {
    Session.set('length', Math.floor((Math.random() * 20) + 1));
  }
});

Template.resultSession.helpers({
  returned: function () {
    return Session.get('length');
  }
});

Template.resultMethod.helpers({
  returned: function() {
    Meteor.call('returnArray', Session.get('length'), function (err, res) {
      return res.length;
    });
  }
});

Server-side:

Meteor.methods({
  'returnArray': function (length) {
    var arr = [];
    arr[length - 1] = 0;
    return arr;
  }
});

TL;DR

You can look at code and play with it here http://meteorpad.com/pad/AkBZq4ZFjJuQuzztz/Meteor.call-on-Session-change

您可以在这里查看代码并使用它来http://teteorpad.com/pad/AkBZq4ZFjJuQuzztz/Meteor.call-on-Session-change

As you can see, my method accepts number and returns the array with length equal to number.

如您所见,我的方法接受number并返回长度等于number的数组。

The question is how can I make Meteor.call fire each time Session variable changes?

问题是每当Session变量发生变化时,我怎样才能使Meteor.call激活?

P.S. Values are returned to two different templates on purpose

附:值有意返回到两个不同的模板

3 个解决方案

#1


Your reactive code is working perfectly. If you put a console.log in the Meteor.call you will see that the correct answer is coming back from the server.

您的被动代码运行正常。如果你在Meteor.call中放入一个console.log,你会看到正确的答案是从服务器回来的。

Template.resultMethod.helpers({
  returned: function() {
    Meteor.call('returnArray', Session.get('length'), function (err, res) {
      console.log('it came back ' + res.length);
      return res.length;
    });
  }
});

I have put a Session variable into the return from the server, so now you can see that your reactive code works very simply - no need for complicated autorun stuff.

我已经将一个Session变量放入服务器的返回中,所以现在您可以看到您的反应代码非常简单 - 无需复杂的自动运行内容。

<template name="resultMethod">
  <p>Meteor.call returned {{returned}}</p>
</template>

Then in the resultMethod helper:

然后在resultMethod帮助器中:

Template.resultMethod.helpers({
  returned: function() {
    Meteor.call('returnArray', Session.get('length'), function (err, res) {
      Session.set('fromServer', res.length + '');
    });
    return Session.get('fromServer');
  }
});

#2


Like @saimeunt said, use Tracker.autorun

就像@saimeunt所说,使用Tracker.autorun

Templates:

<body>
  {{> resultSession}}
  {{> resultMethod}}
</body>

<template name="resultSession">
  <button>Click me</button>
  <p>Session.get('length') returned {{returned}}</p>
</template>

<template name="resultMethod">
  <p>Meteor.call returned {{returned}}</p>
</template>

And code:

Template.resultMethod.rendered = function() {
    this.autorun(function (){
        Meteor.call('returnArray', Session.get('length'), function (err, res) {
            Session.set('result', res);
        });
    });
}

Template.resultSession.helpers({
  returned: function () {
    return Session.get('length');
  }
});

Template.resultMethod.helpers({
  returned: function() {
    return Session.get('result');
  }
});

Autorun inside rendered stops when the template is not rendered

在未呈现模板时,在渲染内部自动运行停止

#3


You could simply refactor your code to call the Meteor method on click event ?

您可以简单地重构代码以在Click事件上调用Meteor方法吗?

Template.resultSession.events({
  'click button': function () {
    var length = Math.floor((Math.random() * 20) + 1);
    Session.set('length', length);
    Meteor.call('returnArray', length, function (err, res) {
      Session.set('result', res.length);
    });
  }
});

Template.resultSession.helpers({
  returned: function () {
    return Session.get('length');
  }
});

Template.resultMethod.helpers({
  returned: function() {
    return Session.get('result');
  }
});

You could also use Tracker.autorun to track modifications of your Session variable and rerun arbitrary code.

您还可以使用Tracker.autorun跟踪Session变量的修改并重新运行任意代码。

Tracker.autorun(function(){
  var length = Session.get("length");
  console.log("length new value =", length);
});

#1


Your reactive code is working perfectly. If you put a console.log in the Meteor.call you will see that the correct answer is coming back from the server.

您的被动代码运行正常。如果你在Meteor.call中放入一个console.log,你会看到正确的答案是从服务器回来的。

Template.resultMethod.helpers({
  returned: function() {
    Meteor.call('returnArray', Session.get('length'), function (err, res) {
      console.log('it came back ' + res.length);
      return res.length;
    });
  }
});

I have put a Session variable into the return from the server, so now you can see that your reactive code works very simply - no need for complicated autorun stuff.

我已经将一个Session变量放入服务器的返回中,所以现在您可以看到您的反应代码非常简单 - 无需复杂的自动运行内容。

<template name="resultMethod">
  <p>Meteor.call returned {{returned}}</p>
</template>

Then in the resultMethod helper:

然后在resultMethod帮助器中:

Template.resultMethod.helpers({
  returned: function() {
    Meteor.call('returnArray', Session.get('length'), function (err, res) {
      Session.set('fromServer', res.length + '');
    });
    return Session.get('fromServer');
  }
});

#2


Like @saimeunt said, use Tracker.autorun

就像@saimeunt所说,使用Tracker.autorun

Templates:

<body>
  {{> resultSession}}
  {{> resultMethod}}
</body>

<template name="resultSession">
  <button>Click me</button>
  <p>Session.get('length') returned {{returned}}</p>
</template>

<template name="resultMethod">
  <p>Meteor.call returned {{returned}}</p>
</template>

And code:

Template.resultMethod.rendered = function() {
    this.autorun(function (){
        Meteor.call('returnArray', Session.get('length'), function (err, res) {
            Session.set('result', res);
        });
    });
}

Template.resultSession.helpers({
  returned: function () {
    return Session.get('length');
  }
});

Template.resultMethod.helpers({
  returned: function() {
    return Session.get('result');
  }
});

Autorun inside rendered stops when the template is not rendered

在未呈现模板时,在渲染内部自动运行停止

#3


You could simply refactor your code to call the Meteor method on click event ?

您可以简单地重构代码以在Click事件上调用Meteor方法吗?

Template.resultSession.events({
  'click button': function () {
    var length = Math.floor((Math.random() * 20) + 1);
    Session.set('length', length);
    Meteor.call('returnArray', length, function (err, res) {
      Session.set('result', res.length);
    });
  }
});

Template.resultSession.helpers({
  returned: function () {
    return Session.get('length');
  }
});

Template.resultMethod.helpers({
  returned: function() {
    return Session.get('result');
  }
});

You could also use Tracker.autorun to track modifications of your Session variable and rerun arbitrary code.

您还可以使用Tracker.autorun跟踪Session变量的修改并重新运行任意代码。

Tracker.autorun(function(){
  var length = Session.get("length");
  console.log("length new value =", length);
});