当 Meteor.call() 立即更新数据库时,如何延迟响应式 Meteor 模板更新 HTML 模板变量? [英] How can I delay a reactive Meteor template from updating an HTML template variable, when a Meteor.call() updates the database instantly?

查看:23
本文介绍了当 Meteor.call() 立即更新数据库时,如何延迟响应式 Meteor 模板更新 HTML 模板变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个基诺游戏.当用户按下开始按钮时,Meteor.Call() 会执行该卡片选择的所有内容.包括更新用户余额.我有一个中奖号码的 setTimeout,以便它们显示大约 20 秒.问题是,当拨打电话时,余额立即更新,然后数字开始显示延迟.我不熟悉如何解决这个问题.我很感激任何帮助.

I'm developing a keno game. When the user presses the start button a Meteor.Call() executes everything for that card pick. Including updating the user balance. I have a setTimeout for the winning numbers, so that they display over a period of about 20 seconds. The problem is that when the call is made, the balance updates instantly, and then the numbers start displaying with the delay. I not familiar with how to solve this. I appreciate any help.

服务器端:

Meteor.methods({
    process: function(){
        // generate numbers
        // update user balance
    }
});

客户端:

Template.keno.events({
    'click #start' : function(){
        Meteor.call('process',function(err,numbers){
            //setTimeout on displaying numbers
            // as setTimeout displays numbers, balance already updated. I need to delay
            // the balance update, until all numbers are displayed. 
            // otherwise, the player see that they won before all numbers come out.
        });
    }
});

** 更新 **

我需要的唯一帮助是了解如何使 {{balance}} 之类的变量无反应,直到我完成 setTimeout,然后更新它.我应该使用会话吗?我不应该使用模板变量,而是使用 jquery 插入余额吗?这只是一个简单的解决方案,困难在于我不知道我在寻找什么功能/方法可以帮助我在设定的时间内关闭反应性,然后在 Meteor.call() 之后更新因为然后数字完成它是setTimeout.

The only help I need is to understand how to make a variable like {{balance}} unreactive, until I finish the setTimeout, and then have it update. Should I be using sessions? Should I not use a template variable and instead, insert the balance with jquery? It's just a simple solution, the difficulty is that I don't know what function / method I'm looking for that can help me turn off the reactivity for a set amount of time, and then update, after the Meteor.call() for then numbers finishes it's setTimeout.

推荐答案

如果我正确理解您的情况,您需要在您决定的时间与集合获得结果的时间设置模板 {{balance}} 表达式从服务器.所以你可以在你喜欢的时候使用 Session 来设置一个值.下面是一个例子:

If I understand your situation correctly, you need the template {{balance}} expression to be set at a time you decide vs. when the collection gets a result from the server. So you could use Session to set a value when you like. Below is an example:

<body>
  {{> game}}
</body>

<template name="game">
  <button id="process">Process</button>
  <div>{{firstNumber}}</div>
  <div>{{secondNumber}}</div>
  <div>balance: {{balance}}</div>
</template>

if (Meteor.isClient) {
  Template.game.events({
    'click #process': function (e, tmpl) {
      Meteor.call('process', function (err, result) {
        Session.set('firstNumber', result[0]);
        setTimeout(function () {
          Session.set('secondNumber', result[1]);
          Session.set('balance', result[0] + result[1]);
        }, 2000);
      });
    }
  });

  Template.game.helpers({
    firstNumber: function () { return Session.get('firstNumber'); },
    secondNumber: function () { return Session.get('secondNumber'); },
    balance: function () { return Session.get('balance'); }
  });
}

if (Meteor.isServer) {
  function randomNumber () {
    return Math.floor(Math.random() * 100);
  }
  Meteor.methods({
    process: function () {
      return [randomNumber(), randomNumber()];
    }
  });
}

这篇关于当 Meteor.call() 立即更新数据库时,如何延迟响应式 Meteor 模板更新 HTML 模板变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆