Thanks Steve for your response, I know this is very old, but I still need the solution.
Actually, When hitting the ‘refresh button’ in the browser it do nothing, but if I navigate to another page and hit the “back button” on the browser things get messy and the code inside don’t re-run. and in this situation, after hitting back, if I tried even to refresh the page, the code also won’t re-run.
anybody know what is the best way to do this, I’ve to initialise some code every time the page renders?
It will help you getting some answers if you posted a repro (preferable) or at least some code snippets and details of your project… i.e., are you using a router for page nav? which one? what causes your template to be created (your html and template insertion code)?
the interesting part is that the problem only happens within nav dynamic template section, I even tried to put the nav code inside main template section and it worked well!
I even tried to initialize for nav on its own, but didn’t solved it.
This is where I would start: I would remove any sort of reactivity out of the route (tracker.autorun … ). And also the Template onRendered for the layout template(s). Leave only the BlazeLayout.render() - one only in each route.
Is the nav template changing or are you expecting the same nav template to re-render just because you change routes? If the later, then that will not happen. onRendered will only execute once per template instance after is it added to the DOM.
I’m not really sure what exactly you are trying to resolve here… back to you original question:
at first load things go fine, when reloading the page this initialisations don’t work!
Assuming that when you say “reloading the page” you mean change route, if the template instance is already attached to the DOM, them there will be no onRendered.
If you need to run the selectize code again because the data context for the dropdown has changed then you should warp those bit inside an autorun referencing the data context, not by trying to get onRendered to execute again.
Thank you very much @hluz for your efforts, the selectize code was the first thing I faced the problem with, and solved it as you mentioned, but the problem now, as I mentioned later, in initializing things that don’t depend on data context, like zurb foundation ui framework, and the recommended place is inside onRendered as per their docs.
In general, it is better to initialise these ui frameworks from within onRendered on the lower-level templates. If the template data context can change without triggering the onRendered code, then use a template instance autorun to wrap a reference to the data context and the initialisation code.