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
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.
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?
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.
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…
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?
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 :