Injecting willTransition hook in all routes

I would like to inject a willTransition and didTransition hook in all of my routes for debugging and understanding better how Ember.js works.

For example, it might look something like the following:

willTransition: function(t) {
    console.log('IndexRoute: willTransition(' +
        'isActive='      + t.isActive +
        ',resolveIndex=' + t.resolveIndex +
        ',sequence='     + t.sequence +
        ',targetName='   + t.targetName +
    ')');
    return true;
}

From what I understand, this is exactly what the Ember.Application.initializer can be used for.

What is the best way to do this?

A nice code example would be instructive.

Add this routine to a base route and extend all other routes from it?

If this method is already defined in routes you might want to also add this._super(); call to it to include base class functionality.

Yes I guess this would work.

But then again, I’d have to remember to extend all new routes from this base root otherwise it wouldn’t work.

Since I’m lazy by nature, I’d rather not have to do this.

Can I inject this directly into the Ember.Route object without breaking anything?

Have you tried .reopen()? Be sure to call this._super() or make a different property and just listen for .on('willTransition').

http://emberjs.com/guides/object-model/reopening-classes-and-instances/

Ember.Route.reopen({

    logWillTransition: function(){
        console.log('here');
    }.on('willTransition'),

    logDidTransition: function(){
        console.log('here');
    }.on('didTransition')

});

How can I also log the route name from which it is being called? Is there someway to access that?

You can get them from the ApplicationController with controllerFor: http://emberjs.com/guides/understanding-ember/debugging/#toc_get-current-route-name-path

1 Like

Nice catch, i will give it a try.