1

I tried two ways. First is to just define a new route with same pattern, but it gives me error saying "Path already exist".

And then I also tried to get the existing RouteController from the router and change it, but it didn't went well

To be specific, I am trying to override the following route in project Telescope.

https://github.com/TelescopeJS/Telescope/blob/master/packages/telescope-posts/lib/routes.js#L159-L162

kayue
  • 2,546
  • 3
  • 20
  • 16
  • 2
    Sounds like you want to use an `onBeforeAction` hook - https://github.com/iron-meteor/iron-router/blob/devel/Guide.md#using-hooks There can be only one particular `path` defined in Iron Router, but what template that path loads, what data that path has, etc can be defined inside `onBeforeAction` – fuzzybabybunny Jun 30 '15 at 01:36
  • Not the ideal way but it works. Posted an solution below. Thanks. – kayue Jul 01 '15 at 02:15

2 Answers2

1

One approach is to modify existing route options.

// Running it in Meteor.startup is only necessary because Telescope
// defines the route at startup
Meteor.startup(function () {
  // Find a route by name
  var route = Router.routes.posts_default;
  // OR find a route by path
  var route = Router.findFirstRoute('/');

  // Override existing route options
  _.extend(route.options, {
    data: //...
    // Other route options...
  });
});

Another approach is to remove the route and recreate it.

Using a function which removes a route by its name,

function removeRouteByName (routeName) {
  var routes = Router.routes;
  var route = routes[routeName];
  if (!route) return false;  // Returns false if route is not found

  // Remove route from Router
  delete routes[routeName];
  delete routes._byPath[route.path()];
  var routeIndex = routes.indexOf(route);
  if (routeIndex >= 0) routes.splice(routeIndex, 1);

  // Remove route handler from MiddleWareStack
  delete Router._stack._stack[routeName];
  Router._stack.length -= 1;

  return true;  // Returns true when route is removed
}

We can remove and recreate the route by

// Running it in Meteor.startup is only necessary because Telescope
// defines the route at startup
Meteor.startup(function () {
  removeRouteByName('posts_default');

  Router.route('/', {
    name: 'posts_default',  // Use the same name
    //Add your parameters...
  });
});

Try running Telescope in this repository and you should see that the route / has been changed.

xamfoo
  • 151
  • 1
  • 5
  • Tried and didn't work, am I supposed to remove it from `Meteor.startup`. The `removeRouteByName` is causing log exception to me. Also Iron Router is maintaining a `Router.routes._byPath` array as well, if we want to remove a route we should update this array too. – kayue Jul 01 '15 at 02:12
  • Yes it should be removed in `Meteor.startup()` because Telescope defines it there. I fixed `removeRouteByName()` and added a [repository](https://github.com/xamfoo/change-route-in-iron-router/blob/master/changeRoute.js) with a working example. Hope it helps! – xamfoo Jul 01 '15 at 04:41
0

Using onBeforeAction hook will do the job, although not the best way to do it.

Router.onBeforeAction(function () {
  if (Router.current().route.getName()==="posts_default") {
    this.render("posts_list_controller", {
      data: {
        terms: { // Telescope specific view data
          view: 'another_view',
          limit: 10
        }
      }
    });
  } else {
    this.next();
  }
});
kayue
  • 2,546
  • 3
  • 20
  • 16