Skip to content

Instantly share code, notes, and snippets.

@jperl
Last active August 29, 2015 14:12
Show Gist options
  • Save jperl/d4d505005549165ab1d9 to your computer and use it in GitHub Desktop.
Save jperl/d4d505005549165ab1d9 to your computer and use it in GitHub Desktop.
Track previous route in iron router
Routes = {};
/**
* The current route's name
*/
Routes.name = function () {
return Router.current().route.getName();
};
var routeNameAfterLastAction = null;
var previousNameVar = new ReactiveVar(null);
/*
* The previous route's name
*/
Routes.previousName = function () {
return previousNameVar.get();
};
Router.onBeforeAction(function () {
var routeName = Routes.name();
if (routeNameAfterLastAction !== routeName && routeNameAfterLastAction !== previousNameVar.get()) {
previousNameVar.set(routeNameAfterLastAction);
}
this.next();
});
Router.onAfterAction(function () {
// Wait until afterFlush in case there are redirects.
Tracker.afterFlush(function () {
routeNameAfterLastAction = Routes.name();
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment