-
-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Description
The Problem
Under certain circumstances, it is desirable to have some logic in the Router#didTransition hook based on what the previous/current route is. Currently, the only way to obtain this is through the container
App.__container__.lookup("controller:application").get("currentRouteName")`The Request
Would it be possible to get this info as an argument to the callback hook? Maybe even a Transition object indicating the from/to routes?
The Back Story
The use case that led me to this scenario was trying to get Google Analytics tracking on my app and doing something like the Ember Guides suggest. The problem I was running into was that I was getting events doubled up whenever a slow transition occured. This is because didTransition is firing once for the loading substate and a second time once the model promise has resolved, which is desirable and correct behaviour. However, I need to ignore any transition to a route that matches /\.loading$/ for page view tracking purposes:
App.Router.reopen({
notifyGoogleAnalytics: function() {
// Get current route name the "hacky" way
currentRoute = App.__container__.lookup('controller:application').get('currentRouteName');
if (!currentRoute.match(/\.loading$/)) {
// Track pageview only if not a `loading` substate
return ga('send', 'pageview', {
'page': this.get('url'),
'title': this.get('url')
});
}
}.on('didTransition')
});Ideally, this could be a little nicer with something like:
App.Router.reopen({
notifyGoogleAnalytics: function(transition) { // Assuming transition = {from: 'original.route', to: 'target.route'}
// Obtain the current route from the arguments, or from some property on the Router
currentRoute = transition.to;
if (!currentRoute.match(/\.loading$/)) {
return ga('send', 'pageview', {
'page': this.get('url'),
'title': this.get('url')
});
}
}.on('didTransition')
});