Reputation: 10237
Based on Dan Dascalescu's answer here, I want to implement dynamic templates in Meteor like this:
In main.html:
<body TEXT="#000000">
<div class="container">
{{> mnuScheduler}}
{{> Template.dynamic template=currentTemplate}}
</div>
</body>
In main.js:
Template.body.helpers({
currentTemplate: function () {
return Session.get('curTemplate');
}
});
But obviously I need to initiate my Session variable 'curTemplate' to the first template I want to display, like:
Session.setDefault('curTemplate', 'tblScheduler');
...and then dynamically set it elsewhere like this:
Session.set('curTemplate', somethingElseBasedOnContext);
My question is, where does the initial (setDefault) belong? Should it be i main.js, along with Template.body.helpers (where I also have my Meteor.subscribe("bla") calls?
Upvotes: 0
Views: 674
Reputation: 3240
Either of the following three locations will work:
if (Meteor.isClient) {
Session.set('curTemplate', 'tblScheduler'); // <---
console.log('Meteor.isClient');
Meteor.startup(function() {
//Session.set('curTemplate', 'tblScheduler') // <---
console.log('Meteor.startup');
});
Template.body.onCreated(function() {
//Session.set('curTemplate', 'tblScheduler') // <---
console.log('Template.body.onCreated');
});
Template.body.helpers({
currentTemplate: function() {
return Session.get('curTemplate');
},
});
}
With this code the browser log shows the order these are called on startup:
Meteor.isClient
Template.body.onCreated
Meteor.startup
Meteor.startup will not run until after the DOM is ready however that is not a factor in this case.
Meteor is very flexible in how you can structure your app, so it's best to be consistent in your approach.
Here you could decide that startup code goes in the Meteor.startup block, or you could decide that as it's required to render the body template, Template.body.onCreated is the right place for it. But try to be consistent!
Upvotes: 2