javascript - meteor 迭代一组 session 以检查值是否已更改

标签 javascript session twitter-bootstrap meteor iteration

我得到了那些 session :

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);

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

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);
});

我的 HTML:

<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;
}

关于javascript - meteor 迭代一组 session 以检查值是否已更改,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19479743/

相关文章:

javascript - jPlayer中是否有音频淡入的方法

javascript - 使用 'attr()' 时选择之间的差异

asp.net - 为什么这个函数没有执行?

页面之间丢失的 PHP session - 行为因服务器而异

html - 使图像保留在同一位置的 div 中

html - Bootstrap 垂直居中对齐两个文本 block

javascript - Bootstrap 3 的 Ladda UI 不工作?请查看我的代码并告诉我修复

internet-explorer - Internet Explorer 随机丢弃 cakePHP 中页面之间的 session

ruby - 导轨 : Advantages of storing session in database?

javascript - 此页面如何不与 Bootstrap 3.3.4 中的 Collapse.js 一起使用?