Search code examples
meteorroutesiron-routermiddleware

meteor iron-router and getting the names of all routes for the accounts-entry package


The popular accounts-entry package has an iron-router related bug in it. I believe the later versions of iron-router changed to work better as middleware and so a call to Router.routes

At line 87 of this file the following code is used:

_.each Router.routes, (route)->
  exclusions.push route.name
# Change the fromWhere session variable when you leave a path
Router.onStop ->
  # If the route is an entry route, no need to save it
  if (!_.contains(exclusions, Router.current().route?.getName()))
    Session.set('fromWhere', Router.current().path)

Unfortunately it does not seems like doing an _.each on Router.routes is a solution that works anymore because Router.routes does not return and object with .name properties in it.

How would you get the name of all the routes with the latest iron-router?


Solution

  • This one is a little tricky : in the latest version of iron:router, Router.routes is now defined as an array of functions.

    Thing is, functions already have a default name property in JS which contains the name the function was assigned on definition.

    var myFunc = function funcName(){...};
    console.log(myFunc.name); // == "funcName"
    

    Fortunately, there is a getName method defined on the route items of the array and you can use this piece of code to iterate over all routes and get their name :

    _.each(Router.routes, function(route){
      console.log(route.getName());
    });