[RESOLVED] Access to subscribed data in an event function


I just migrate my subscriptions from IronRouter to my templates. It’s working but I have a problem.

On mly template sessionDate, I subscribe to the sessionDate I have to display:

Template.sessionDate.onCreated(function () {
  this.autorun(() => {
    const sessionID = Router.current().params._ID;
    this.subscribe( 'sessionDates.one', sessionID );

Then I made a template helper to access to the data of this sessionDate (maybe this is not the good way):

  "sessionDate": function(){
    const sessionID = Router.current().params._ID;
    var sessionToDisplay = Sessions.findOne({ "_id": sessionID });
    var sessionDate = SessionDates.findOne({ sessionID: sessionID, last: true });
    sessionDate.infos = sessionToDisplay;
    return sessionDate;

So in my template I always call the helper to have access to the sessionDate data. BUT now I’m stuck with a template event that need some of this sessionDate data…

  'click .session-menu .share-session': function (event, instance) {
    Router.go('session.share', { _ID: sessionDate.infos._id });
    // sessionDate is undefined!

How can I access to the sessionDate data in this event function?

Thanks for any help! :slight_smile:

Attach it to the Template.sessionDate.onCreated:

Template.sessionDate.onCreated(function () {
  this.sessionDate = {};
  this.autorun(() => {...

and then refer to that elsewhere. In a helper, that would be Template.instance().sessionDate. In an event, you make use of the instance parameter in the function statement: instance.sessionDate.

Used like that, sessionDate is not itself reactive (but probably doesn’t need to be). If you do need reactivity, use this.sessionDate = new ReactiveVar({}); and refactor elsewhere.


Perfect answer! Thanks!

1 Like