meteor 遍历一组会话以检查值是否已更改

meteor iterate over a set of Sessions to check if the value have changed

本文关键字:检查 是否 会话 遍历 一组 meteor      更新时间:2023-09-26

>我得到了那些会话:

Session.set("group_name",false);
Session.set("group_date",false);
Session.set("group_friends",false);
Session.set("group_location",false);
Session.set("group_rules",false);
Session.set("group_desc",false);
Session.set("group_save",false);

我正在使用引导进度条,它只需要更改宽度属性。我正在尝试实现类似于 Session.get 的操作,这意味着我想检查其中一个会话中是否有更改,以便我可以增加进度条的宽度。我试过做这样的事情:

Meteor.render(function(){
    prog = 0 ;
    prog = prog;
    for( var i in Session.keys){
        if(Session.keys.hasOwnProperty(i) && Session.keys[i] != "false"){
            prog  = prog + 1*15;
        }
    }
    return console.log(prog);
});

我的网页:

<div class="bar" style="width: {{prog}}%;">

这是行不通的。我错过了一些东西,但我不知道是什么。

我对Meteor.render不是很熟悉,但是从文档中它返回了一个反应式片段,然后需要将其附加到DOM中。我猜这就是它不起作用的原因。话虽如此,您真的不需要直接致电Meteor.render(可能永远)。您可以仅使用模板和帮助程序来执行此操作。这是一个完整的工作示例:

测试.html

<body>
  {{> test}}
</body>
<template name="test">
  <div id='progress-wrapper'>
    <div id='progress' style='width: {{progress}}%;'></div>
  </div>
</template>

测试.js

var PROGRESS_VARS = ['group_name', 'group_date', 'group_friends',
'group_location', 'group_rules', 'group_desc', 'group_save'];
Template.test.created = function() {
  _.each(PROGRESS_VARS, function(p) {
    Session.set(p, false);
  });
};
Template.test.helpers({
  progress: function() {
    var total = 0;
    var length = PROGRESS_VARS.length;
    _.each(PROGRESS_VARS, function(p) {
      if (Session.get(p)) {
        total += 1;
      }
    });
    return Math.ceil(100 * total / length);
  }
});

测试.css

#progress-wrapper {
  border: 1px solid #000;
  margin-top: 50px;
  width: 50%;
}
#progress {
  background-color: #008000;
  height: 50px;
}