Search code examples
meteoriron-router

Session variable resetting between pages


I'm learning Meteor and I'm trying to use the counter example with Iron Router. Everything is working in the counter page and the routes too, except that when I'm navigating between pages and come back to the counter page, the counter resets to 0 (after I've clicked and incremented the counter). The HTML is:

<template name='counter'>
    <button>Click Me</button>
    <p>You've pressed the button {{counter}} times.</p>
</template>

and the javascript is:

Session.setDefault("counter", 0);

Template.counter.helpers({                               
  counter: function () {
    return Session.get("counter");
  }
});

Template.counter.events({                                
  'click button': function () {
    Session.set("counter", Session.get("counter") + 10);
  }
});

I tried to put

Session.setDefault("counter", 0);

inside a template.counter.rendered/template.counter.created but it's the same. Does anyone knows how to guarantee that session values can be preserved?


Solution

  • It seems that everything is working as expected and it was my problem: as I was trying to test dynamic routes, sometimes I changed the URL manually on the browser instead of using the "go back" button or navigate using my own page links. And, as it is supposed, by doing that I'm refreshing the page, and everytime I refresh a page I loose session values... Hope this could be useful to someone else.