Access to data in different parts of a template


#1

Hello!

I used to subscribe to the data from the router part. But I wanted to move that part to the template part. But I can’t found a way to access to the data from different parts.

In the HTML part, I found that the best way is to do a helper… OK

In the template I always subscribe to the data in the created part. Like this

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

But how to access the data (sessionDate here) from the helpers and the events parts?

Thanks!


#2

I see you are using ES6 arrow function and const, so must be using a recent version of Meteor.
You should use Template.xxx.onCreated() instead of Template.xxx.created.

To access the data of a Template, you can directly use your data keys on your template Object (for example MyTemplateObject.myData)

For events, you can get this Object by using the second parameter of the function, and for helpers by using Template.instance(). See steps 7.3, 7.4 and 7.5 of this tutorial.

You also have Template.currentData() or <templateInstance>.data but I think, in your case, that it is not what you are looking for.


#3

Hi @borntodev!
Yes exactly, I have to migrate the onCreated declaration.

For the data in the template, I’m still not sure what todo. I understand how to retrieve it know, thanks to you but where do I have to define the data template and how?


#4

Which router are you using?
Anyway, you have to use a Collection within a helper and return the data from the helper, then call the helper in the template.


#5

I’m using IronRouter. I want to switch to FlowRouter. Is for that reason, I’m putting the subscriptions in the template. I used to put the data from the router in the router part but now that I switch to template subscription I don’t see how to add the data to the template…


#6

Subscribe at onCreated

Template.Layout_protected.onCreated(function () {
  this.subscribe('Meteor.users.profile');
});

Query for data in helper, usually call find or findOne method from a collecction in the helper definition:

Template.Layout_protected.helpers({
  currentUserEmail() {
    let user = Meteor.user();
    return user && user.services && user.services.facebook && user.services.facebook.email;
  }
});

#7

It’s exactly what I did. But I get a problem to access a data from a event for example. I can copy/paste the helper that get my data but it might be an easiest solution to get the data in a template event function, no?


#8

Like you did, in the .onCreated(), is correct.


#9

But my sessionDate data is empty in the onCreated… When and where I can fill it?
Or maybe I’m missing something :thinking:


#10

Your data is empty because you are assigning an empty Object.

this.sessionDate = {};

I think you should move this statement outside your .autorun().

.

I already answered to that : see my first reply. (I just rephrased it a bit)


#11

Thanks @borntodev for your patience :stuck_out_tongue:
Is that portion of code good?

Template.sessionDate.onCreated(function () {
  this.sessionID = Router.current().params._ID;
  this.autorun(() => {
    this.subscribe( 'sessions.one', this.sessionID );
  });
  this.sessionDate = getSessionDateInfos(this.sessionID); // Is the data subscribed here??
});

Thanks :+1:


#12

In this case no, if you want to update sessionDate each time _ID is changed you need to keep it in your .autorun() . (I though you just wanted to initialise an empty Object, sorry).

Template.sessionDate.onCreated(function () {
  this.autorun(() => {
    // the function will rerun if it depends on reactive data sources
    // see : https://docs.meteor.com/api/tracker.html#Tracker-autorun

    this.sessionID = Router.current().params._ID;
    this.subscribe( 'sessions.one', this.sessionID );
    this.sessionDate = getSessionDateInfos( this.sessionID );
  });
});

If you only use sessionID in your .autorun(), save it with const sessionID = ... . (like you did before), no need to bind it to the template if you do not reuse it elsewhere.

.

To check when a .subscribe() is done, you can add a callback in the parameters :


#13

Perfect! I will refactor my templates in that way!