0

Using Meteor 0.9+.

Is there a way to instantiate a session as soon as the page renders?

I have a dynamic list of names that display upon clicking a .li element using the click event. This is fine. But I would like the user now to see at least one list, i.e. as if they have already clicked one of the .li elements when they land on the page.

Template.nameList.events({
'click li.title': function(e) {
    e.preventDefault();
    Session.set('postId', this._id);
    var selectedId = Session.get('postId');
}
});
meteorBuzz
  • 3,110
  • 5
  • 33
  • 60
  • 1
    If your using iron:router try taking a look at Chris Mather's talk about it's new features. There is a good bit about global and controller state in there. https://www.youtube.com/watch?v=SLd6fDi5UYE – Will Parker Oct 28 '14 at 11:46
  • Alternatively checkout this similar question: http://stackoverflow.com/questions/11167390/template-onload-event-for-meteor-js – Will Parker Oct 28 '14 at 11:47
  • I'm looking into this too...thanks Will. – meteorBuzz Oct 28 '14 at 12:06

1 Answers1

1

You could use template.created or template.rendered callback:

Template.nameList.rendered = function() {
  Session.set('postId', this.data.someId);
};

You could also use IR onBeforeAction callback:

NameListRouter = RouteController.extend({
  onBeforeAction: function() {
    Session.set('postId', this.params.someId);
  };
});
Hubert OG
  • 19,314
  • 7
  • 45
  • 73
  • this.data.someId may one of of the value in the .li elements. However, Im getting the following error, Cannot read property 'someId.' – meteorBuzz Oct 28 '14 at 12:17
  • Well, change `someId` to the name of your actual data! – Hubert OG Oct 28 '14 at 12:20
  • My bad, I passed on a variable containing a value at index position 0 of the array so it will always be populated. The rendered example works perfectly for what I need. – meteorBuzz Oct 28 '14 at 12:30
  • Can you recommend a method to preserve the session? (store it in the browser). This is so that if the browser is refreshed, the session will still be the same. – meteorBuzz Oct 28 '14 at 13:02