简体   繁体   中英

How to change a session value after a click event, so that a page refresh does not reset the session on Meteor

I am very close to having my app perform as I want, however there is one little issue I am having. I cant seem to get the Session.set to stay after a click event and the user reloads the page. I am basically trying to build a age gate for viewing a alcohol vendors site.

You can view the site here , when you click the link it hides the map, but when you refresh it comes back, I want it to hide the map after the initial click and after refresh, but the initial click is needed.

You can view my javaScript below

Template.homePage.helpers({
   // because the Session variable will most probably be undefined the first time
   data: function(){

         return !Session.get("enter");

   }
});

Template.homePage.events({
  'click a' : function(){
    alert("removeMap");
    Session.set("enter", false);
  }
});


Template.homePage.rendered = function(){
  Session.set("enter", true);
}

**

JS Update

**

Template.homePage.helpers({
   // because the Session variable will most probably be undefined the first time
   data: function(){

         return !Session.get("enter");

   }
});

Template.homePage.events({
  'click a' : function(){
    alert("removeMap");
    Session.setPersistent("enter", false);
    console.log(Session.get("enter"));
  }
});



Meteor.startup(function () {
    Session.setTemp("enter", true);
    console.log(Session.get("enter"));
});

and my template

<template name="homePage">



{{#if data}}

{{> postsList}}

{{ else }}

<h1>Choose a Province:</h1>

{{> map}}

<a href="#">Click Me</a>

{{/if}}



</template>

The thing you asked is already here and here .

Also, you can use this package if you want your session to be persistent

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM