Search code examples
meteoriron-router

Router.after is deprecated, is there a replacement global after hook?


I want to change the title of the page after any route has been rendered, to the name of that route. This is the code I was using before:

Router.after(function(){document.title = this.route.name;});

The manual mentions using onAfterAction inside an individual route, but I'd like to do it globally?


Solution

  • You must have missed this : http://eventedmind.github.io/iron-router/#using-hooks

    The correct syntax is straightforward :

    Router.onAfterAction(function(){
      // your hook definition
    });
    

    Note : The guide is for iron:router@1.0.0-pre2 which must be added to your app explicitly like this :

    meteor add iron:router@1.0.0-pre2
    

    But the Router.onAfterAction works fine in iron:router@0.9.X too.

    I suggest using this.route.getName() instead of this.route.name, see more about this issue here :

    https://github.com/EventedMind/iron-router/issues/878