Last active
August 29, 2015 14:12
-
-
Save jperl/d4d505005549165ab1d9 to your computer and use it in GitHub Desktop.
Track previous route in iron router
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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