Variable name in iron router - meteor

I want to assign a variable to a route's name property in an iron router route in my meteor application but whenever I try to do so the application crashes.
Router.route('/:tpl', function(){
this.render(this.params.tpl)
}, {
name:variableName // I tried name:this.params.tpl also
});
The main purpose of this is to set the document title of the page on the basis of the route name by below code
Router.onAfterAction(function() {
document.title = Router.current().route.getName();
});
Whenever I set the route name to a string the code works but when I assign a variable to route name then the code breaks.
Note - I'm using the latest version of Meteor and Iron Router.

You can't have a variable route name. The route name is used by iron router to identify the route, think of it like the route's ID.
What you could do for your purpose, as you apparently want to have the template name in the title:
Router.onAfterAction(function() {
document.title = this.params.tpl;
});

Related

How to redirect from `/` to `/foo/<id>` using FlowRouter and MeteorJS?

In my scenario, I want everyone that visits our root URL to be auto-redirected to a url containing a document for collaboration and instant gratification.
Here, the router.coffee code is:
FlowRouter.route '/',
action: ->
console.log "I'm home!"
FlowRouter.go 'myProject'
name: 'myHome'
FlowRouter.route '/my/:projectId',
subscriptions: (params) ->
#register 'currentProject', Meteor.subscribe 'project', params.projectId
action: ->
BlazeLayout.render 'myBody'
name: 'myProject'
I want the root URL to redirect to /my/:projectId but I'm unsure of how to retrieve the auto-generated projectId and redirect using with either FlowRouter.go or FlowRouter.redirect.
Is this possible?
If yes, how?
Thanks for your help!
Since the data may not be available when the route action execute,
the best is to re-route at the template level.
It might be a good idea to use the Template.[name].onCreated() function
and put inside it something like the following code:
pID = ... // Get the user project ID from wherever you saved it
var params = {projectId: pID};
// Set the project URL including the :projectId parameter and re-route the user
FlowRouter.go("myProject", params);

Iron Router: How to Provide Same Data to Multiple Routes

It's evident how to provide route-specific data, i.e. through the use of a controller:
PostController = RouteController.extend({
layoutTemplate: 'PostLayout',
template: 'Post',
waitOn: function () { return Meteor.subscribe('post', this.params._id); },
data: function () { return Posts.findOne({_id: this.params._id}) },
action: function () {
this.render();
}
});
But how does one provide data for the application in general? In the case that every route needs to be subscribed to the same subset of information, so that the pub/sub doesn't need to be re-done on every route change. Thanks!
It sounds to me like you are looking for a completely general publication/subscription scheme so that you do not have to define the waitOn/data option combination for every single route or route controller that you define. In that case, you can simply publish a given set of data on the server like so:
Meteor.publish('someData', function() {
return SomeDataCollection.find({});
});
and subscribe to that set of data on the client like so:
Meteor.subscribe('someData');
With this publication/subscription pair setup, you will have access to the data provided in all routes. You just have to make sure that you check for non-existent data in your code to handle the first load of any given template when the data has not been loaded on the client yet. In this manner, you would never have to actually define a the waitOn and data options for any route or route controller.
If you would like to utilize Iron Router in a different way than through route controllers, you also have the option of waiting on one/many subscriptions globally by using the Router.configure({}); function. To use the example above:
Router.configure({
waitOn: function() {
return Meteor.subscribe('someData');
}
});
For information about this route option and all of the other options that you have available at a global level, check this out.

Confusion over creating routes in Iron Router

I have a route defined as such:
Router.route('/posts/:_id/:commentsLimit?', {
name: 'PostTemplate',
controller: PostTemplateController
});
My question is why when I define a new route for editing the post, it is getting redirected to the route above?
Router.route('/posts/edit/:_id', {
name: 'PostEditTemplate',
controller: PostEditTemplateController
});
The url /posts/edit/anIdOfSomeKind is matched by both your routes. In these cases, Iron Router will pick the route that matches the url and was created first. So changing the order in which your routes are created will probably fix your problem.

Force iron-router to go to current route

I have created a route like:
this.route('design.create', {
path: '/design',
template: 'Design',
I have wroten some waitOn, data, onBeforeAction, onStop related and executed when the route is loaded.
I have a "strange" use case:
Where I'm already in the route design.create I would like re-init it (execute again waitOn, data, onBeforeAction, onStop).
A simple: Router.go('design.create') not works...I guess because I'm already on the same route.
Seems that:
Router.current()._computation.invalidate()
Do what I want...but it's not very elegant call a private method
Set a dependency by using a session variable in the onBeforeAction and change the session variable when you what the current route to get rerun:
...
onBeforeAction: function() {
Session.get('dependOnMe')
// the acutal onBeforeAction-code goes here
}
...
...
// somewhere else
Session.set('dependOnMe', new Date())
// will trigger the onBeforeAction again
...

trying to use params on main route (/) of my meteor app

I'm trying to use params on my main route. But in fact the params are not set and they are used in the path :
Router.map(function funcClientRouterMap(){
this.route('home', {
path: '/:_redirect?',
action: function funcClientRouterMapAction(){
console.log(this.path, this.params);
}
})
});
now if i try manual redirection here is what i get :
Router.go('home'); // it redirects on / => ok
Router.go('home', {_redirect: test}); // this.path = /test, and this.params is empty
How can i use _redirect like a params and not a route ?
Thanks
Router.go accepts a path as its first argument (per the docs). So if you were trying to programmatically cause the same result as the user going to /redirectMeSomewhere, you just use:
Router.go('/redirectMeSomewhere');
And this.params._redirect should be 'redirectMeSomewhere'.
Note that like #apendua implies, if you have other routes it could cause chaos to have a route defined as /:anything, because the other routes may never get triggered. If the above doesn't do the trick, try commenting out all your other routes to see if that changes anything.

Resources