Reputation: 2566
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.
Upvotes: 1
Views: 829
Reputation: 151
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.
Upvotes: 1
Reputation: 2566
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();
}
});
Upvotes: 0