余额仍停留在5000

Balance is still stuck at 5000

本文关键字:5000 停留在 余额仍      更新时间:2023-09-26

>我已经尝试了我能想到的一切,无论如何我的平衡都不会改变。我目前的代码是:

if (Meteor.isClient) {
  Session.setDefault('num', Random.fraction());
  Session.setDefault('bal', 5000)
  Template.hello.helpers({
    num: function () {
      return Session.get('num');
    }
  });
  Template.Balance.helpers({
    bal: function () {
      return Session.get('bal');
    }
  });
  Template.hello.events({
    'click button': function () {
      Session.set("num", Random.fraction());
    }
  });
   Template.TrueFalse.helpers({
  'trueFalse': function(){
    return( Session.get('num') > 0.5 ? true : false )
  }
});
Template.Balance.events({
  'click button': function() {
    if ( Session.get('num') > 0.5 ) {
      Session.set('bal', Session.get('bal') + 1);
    } else {
      Session.set('bal', Session.get('bal') - 1);
    }
  }
});
}
if (Meteor.isServer) {
  Meteor.startup(function () {
  });
}

我尝试了一些以前的解决方案,但似乎都没有奏效。

我认为这可能与生成随机数的事实有关,并检查它是否同时大于或小于 .5,因为两个函数都是"单击按钮",但无论如何我不知道如何解决这个问题。

有人可以编写修复代码,我会弄清楚你做了什么吗?

谢谢。

顺便说一句,如果它有帮助,这是 HTML 文件:

<head>
  <title>RNG</title>
</head>
<body>
  <h1>{{> Balance}}</h1>
  {{> hello}}
  <h1>{{> TrueFalse}}</h1>
</body>
<template name="hello">
  <button>Click Me</button>
  <p>You've pressed the button {{num}} times.</p>
</template>
<template name="TrueFalse">
    {{#if trueFalse}}
        You Win!
    {{else}}
        You Lose!
    {{/if}}
</template>
<template name="Balance">
    Balance: {{bal}}
</template>

我没有真正使用过Meteor,但它看起来像这样:当您为模板定义事件时,事件选择器仅在模板匹配。因此,永远不会触发在余额模板中绑定的click button事件,因为余额模板中没有按钮。

您可以在 Hello 模板的click button事件中更新余额:

  Template.hello.events({
    'click button': function () {
      Session.set("num", Random.fraction());
      if ( Session.get('num') > 0.5 ) {
        Session.set('bal', Session.get('bal') + 1);
      } else {
        Session.set('bal', Session.get('bal') - 1);
      }
    }
  });