Im trying to load a set of items from a database based on the ID contained in the URL. That way I can share the link with other people and they can still see what they are supposed to. Perhaps this is the wrong way to go about it?! Any help would be appreciated.
There's a number of ways to go about doing this. Without knowing the structure of your URL, I'll assume it's something like http://myapp.com/page/[ID]
, where [ID] is the ID in question. In that case, your Iron Router route would look like so:
Router.route('/page/:_id', {
name: 'page'
});
If that's the case, and assuming you don't have any data to pass along to the page, you can access the ID directly from the client by calling Router.current().params._id
, for instance in a template helper:
Template.myTemplate.helpers({
currentId: function() { return Router.current().params._id; }
});
Alternatively, the Router has access to the route parameters, so you can pass the ID along as data in the Route function:
Router.route('/page/:_id', {
name: 'page',
data: function() { return {_id: this.params._id; }
});
That data is then passed in as the data context for the template, so you can access that directly in the template like so:
<template name='myTemplate'>
The current ID is {{_id}}
</template>
Finally, assuming you are actually pulling something out of the DB and using that as your data context, you can access it directly in the template as well. Here, I assume you have a collection called "Pages" that you want to pull from:
Router.route('/page/:_id', {
name: 'page',
waitOn: function() { return Meteor.subscribe('page', this.params._id); }
data: function() { return Pages.findOne({_id: this.params._id});
});
Then you can access that directly in your template as before:
<template name='myTemplate'>
The current ID is {{_id}}
</template>
For more information on routing with Iron Router, I suggest you look at the Iron Router Guide.