简体   繁体   中英

Meteor: instantiate a Session without the click event

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, ie 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');
}
});

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);
  };
});

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