I want to execute a function when any page loads.
Things like Meteor.startup()
and Template.myTemplate.onRendered()
are not what I want because they are triggered only once, when the app is loaded.
Basically I need an event that is triggered every time the URL changes, is there one?
You can use onRun
or onBeforeAction
to run arbitrary code on every route change.
Router.onRun(function(){
console.log('onRun', this.current().route.getName());
this.next();
});
Router.onBeforeAction(function(){
console.log('onBeforeAction', this.current().route.getName());
this.next();
});
Use this placeholder code to detect when this code will actually run.
onRun
will run only once on every route change. (good for analytics related stuff)
onBeforeAction
will reactively rerun when the current route data context is modified.