Meteor不会保存会话值

时间:2013-04-17 14:13:55

标签: session meteor

我尝试使用Session.get()和Session.set()来检查用户是否单击了按钮。如果实现此代码以检查会话是否已设置。

"click .alt": function(event,template){

        if (Session.get("selected") === false){
            var clicked = event.currentTarget;
            clicked.className += " chosen";
            var data = clicked.dataset; 
            console.log(data);
            Session.set("selected", true);
        if (data.corr == true){
            console.log("Hi");
        }
        else{
            console.log("Not Hi");
        }
    }
    else{
        console.log("session was set to true");
    }
}

始终打印consol.log(数据),但即使data.corr = true,也会打印“Hi”。由于某些原因不明,对我来说,这不起作用。希望有人可以帮助我。

更新 这个工作:

    if (Session.get("selected") == false){
        Session.set("selected", true);
        var clicked = event.currentTarget;

        if (clicked.dataset.corr == true){
            //Recording the time and calculating some points                
            time = new Date;
            time = time.getTime();
            time = 3000-Math.floor((time -Session.get("startedTime"))/10 );
            gameCol.insert({team:Session.get("teamnr"), points: time});
            console.log("Correct answer");
        }
        else{
            //0 points for wrong answer
            gameCol.insert({team:Session.get("teamnr"), points: 0});
            console.log("Wrong answer");
        }
    }
    else{
        console.log("You have answered");
    }

这有效:

    if (Session.get("selected") == false){
        Session.set("selected", true);
        var clicked = event.currentTarget;

        if (clicked.dataset.corr == true){
            //Recording the time and calculating some points                
            time = new Date;
            time = time.getTime();
            time = 3000-Math.floor((time -Session.get("startedTime"))/10 );
            gameCol.insert({team:Session.get("teamnr"), points: time});
            console.log("Correct answer");
        }
        else{
            //0 points for wrong answer
            //gameCol.insert({team:Session.get("teamnr"), points: 0});
            console.log("Wrong answer");
        }
    }
    else{
        console.log("You have answered");
    }

唯一的区别是在db中插入错误答案的行是remover。见//gameCol.insert({team:Session.get("teamnr“),点:0});

Upadate 1.1 如果相反,将if / else更改为两个

if (clicked.dataset.corr == true){
            //Recording the time and calculating some points                
            time = new Date;
            time = time.getTime();
            time = 3000-Math.floor((time -Session.get("startedTime"))/10 );
            gameCol.insert({team:Session.get("teamnr"), points: time});
            console.log("Correct answer");
        }
        if(clicked.dataset.corr == false){
            //0 points for wrong answer
            gameCol.insert({team:Session.get("teamnr"), points: 0});
            console.log("Wrong answer");
        }

3 个答案:

答案 0 :(得分:0)

确实有效。我添加了你的代码(删除了一些我不知道它是什么的东西)给一个香草流星项目,它运行得很好。

我使用mrt create创建了一个项目,此代码在每次单击hello-template时将会话设置为true。

if (Meteor.isClient) {

  Meteor.startup(function() {
    Session.set("selected", false);
  });

  Template.hello.greeting = function () {
    return "Welcome to so-q.";
  };

  Template.hello.events({
    "click": function(event,template){

      if (Session.get("selected") === false){
        console.log("session was set to false");
        Session.set("selected", true);
      }
      else{
        console.log("session was set to true");
        Session.set("selected", false);
      }
    }
  });

}

if (Meteor.isServer) {
  Meteor.startup(function () {
// code to run on server at startup
});
}

答案 1 :(得分:0)

尝试使用if (Session.equals('selected', false))代替if (Session.get('selected') === false。它应该工作。

答案 2 :(得分:0)

我想我发现了问题是什么,可能是我对Meteor的了解。我认为当在模板中使用反应数据时,任何绑定模板数据的更改都会触发模板中所有绑定数据的重新计算。会话数据也在模板中绑定的函数中设置和使用。因此,一旦触发重新计算,会话就会再次设置为false。

只要我将模板分成小块,每块都包含一个单独的模板,这种行为就会结束。