Anders Kitson
Anders Kitson

Reputation: 1545

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>

Upvotes: 0

Views: 1181

Answers (1)

sdooo
sdooo

Reputation: 1881

The thing you asked is already here and here.

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

Upvotes: 1

Related Questions