Custom fields in a meteor route - meteor

I have my a number of routes inside my routes.js file and i want to introduce an extra field into it. Below is the route
Router.route('/', {
name: 'home',
controller: 'LoginController',
where: 'client',
action:'index'
});
Since i have a number of routes,i want to go through all the routes and get the route name like this
_.each(Router.routes, function(route){
console.log(route.getName());
});
I want to use the route names to generate links. Links require link names and i have the idea of putting the link names in the routes.
Router.route('/', {
name: 'home',
controller: 'LoginController',
where: 'client',
text: 'Login Link',
action:'index'
});
Is introducing custom fields in the routes allowed in meteor?.

I found out there is a title option but its not in the docs http://iron-meteor.github.io/iron-router/#route-specific-options
and used it this way
Router.route('/', {
name: 'login',
controller:'HomeController',
where: 'client',
title: 'login',
icon: 'this is the icon',
action:'index'
});
and to get the options
Router.routes.forEach(function(route){
console.log(route.getName());
console.log(route.options.title);
console.log(route.options.icon);
});
and this is the result
login
login
this is the icon
so even the custom option icon seems to be working.

Related

Meteor - Flowrouter: generic vs variable route

I want to display posts for different cities, defined by cityId:
FlowRouter.route("/:cityId", {
name: 'postList',
action: function() {
console.log(FlowRouter.getParam("cityId"));
return BlazeLayout.render('mainLayout', {
top: 'header',
body: 'postList'
});
}
});
Alongside with that I of course have generic routes like 'admin', 'signup' and so on.
But when I go to /signup, the postList route gets activated, treating 'signup' word as a city id, and 'signup' is logged in console.
Defining route like FlowRouter.route("/postList/:cityId") is not an option.
Actually, you need to control route definition order.
define the /signup route before the generic one: /:cityId

Match onBeforeAction to only a group of routes using Iron Router for Meteor

Using Iron Router for Meteor, I know I can use the parameter 'only' or 'except' to match named routers when defining an onBeforeAction, like the example in the guide:
Router.onBeforeAction(myAdminHookFunction, {
only: ['admin']
});
Imagine I have a lot of views not needing this on before action, like public pages, and a lot needing it, like admin panel. Is there any way to apply this onBeforeAction to a group of routes without explicit list each one? I mean, for example, apply to all routes starting with '/admin', something like:
only: ['/admin/*']
Or maybe apply the onBeforeAction to a parent route and then define nested routes? (I can't find if Iron Route supports nested routes), something like:
Router.route('/admin/', {
name: 'admin',
onBeforeAction: function() {},
routes: [
{name: 'users', path: '/admin/users', action: function() {}},
{name: 'posts', path: '/admin/posts', action: function() {}}
]
})
I would suggest creating controllers. Create a "base" controller and extend it wherever needed.

Do routes with same controller subscribe to server n times or just once?

I have a tabs with four tab, each has a route with same controller.
All four tabs share the same data, menu, but use different part of the data.
Every time I click a tab, will it do a subscribe to the server?
For example, the first time I click tab1, it will contact server and get the data menu, then I click tab2, will it contact server again to fetch data menu even I have already gotten the data?
If so, how can I avoid this? Maybe I should redesign the code, is there any good ideas?
MenuController = RouteController.extend({
layoutTemplate: 'menuLayout',
waitOn: function () { return Meteor.subscribe('menu', this.params._id); },
data: function () { return Menu.findOne({_id: this.params._id}) },
});
this.route('/menu/tab1', {
name: 'menu.tab1',
template: 'MenuTab1',
controller: MenuController,
});
this.route('/menu/tab2', {
name: 'menu.tab2',
template: 'MenuTab2',
controller: MenuController,
});
this.route('/menu/tab3', {
name: 'menu.tab3',
template: 'MenuTab3',
controller: MenuController,
});
this.route('/menu/tab4', {
name: 'menu.tab4',
template: 'MenuTab4',
controller: MenuController,
});
This has changed somewhat as IR has matured. I believe in the current implementation, if you change between routes which make the same subscription with the same parameters, the subscription will not be stopped and started again. In other words, switching between tabs should not start and stop the subscription (assuming this.params._id remains constant).
You can prove (or disprove) this by adding a console.log('here') to the first line of your menu publisher. When you switch tabs, check the command-line console. If 'here' is printed only once, you have the desired outcome.
Regardless of the outcome, subs-manager is the generally accepted solution for caching subscriptions between routes.

Using Meteor, useraccounts-core ensureSignedIn plugin won't except '/' route

I am trying to use the ensureSignedIn plugin on all routes except for a front 'home' page that has buttons to login to separate parts of the site.
Here's my route for 'home':
Router.route('/', function () {
this.render('home');
});
Here's the line for the plugin and exceptions:
Router.plugin('ensureSignedIn', {
except: ['home', 'atSignIn', 'atSignUp', 'atForgotPassword']
});
Both snippets are from my lib/routes.js file, if that makes a difference.
I've tried adding different route names in the except: portion, and they get correctly excepted, but I can't for the life of me get the 'home' route to not show "Must be logged in".
I've googled and read through gitHub issues and haven't seen anyone else with this problem, so it's probably something I'm doing wrong and not a bug with useraccounts or iron-router.
Set the name of the / route to root, then add that route name to the ensureSignedIn settings:
Router.route('/', {
name: 'root',
template: 'home',
action: function() {
this.render();
}
});
Router.plugin('ensureSignedIn', {
except: ['root', 'atSignIn', 'atSignUp', 'atForgotPassword', 'atResetPwd']
});

How can I set the title dynamically per page with Manuel Schoebel's SEO package?

With Meteor 1.0.3.1 and Iron Router, I need to set the title dynamically for some pages, while defaulting to a certain title for other pages, using Manuel Schoebel's SEO package. How can I accomplish setting a dynamic page title for a certain route?
I've set SEO up generally like this:
Meteor.startup(->
[...]
SEO.config({
title: 'MusitechHub'
meta: {
'description': 'The hub for finding and publishing music technology projects'
}
})
undefined
)
As stated in the package README, you can use an iron:router onAfterAction hook to dynamically set the title to whatever computed value you want :
Router.route("/post/:slug", {
onAfterAction: function() {
var post = Posts.findOne({
slug: this.params.slug
});
SEO.set({
title: post.title
});
}
});

Resources