Search code examples
meteoriron-routermeteor-blaze

Meteor Iron Router. One Route, Multiple Templates. How?


I'm using the latest Meteor and Iron Router. Imagine have a 'customComputer' collection. Computers have three different states: 'ordering', 'building', 'shipped'.

Right now, I'm using three different routes for this, each with a different template

/o/_id
/b/_id
/s/_id

A computer can't be in 2 states at once, so I'd like to have one route. How do I wrangle the templates?

/c/_id

The best I can come up with is to make a "main" template that links to the others. Is this a best practice?

{{#if isOrder}}{{>orderTemplate}}{{/if}}
{{#if isBuilding}}{{>buildingTemplate}}{{/if}}
{{#if isShipped}}{{>shippedTemplate}}{{/if}}

Or dynamic templates

Here's the route:

Router.route('order', {
  path: '/o/:b/:computerId',
  onAfterAction: function() {
    if (this.title) document.title = this.title;
  },
  data: function() {
    if(!this.ready()) return;

    var o = Computer.findOne(this.params.computerId);
    if(!o) throw new Meteor.Error('Computer not found');

    return o;
  },
  waitOn: function() {
    if (!Meteor.userId()) return this.next();  // Don't subscribe if not logged in.
    return [
      Meteor.subscribe('computerById', this.params.computerId),
      Meteor.subscribe('myProfile'),
    ];
  },
});

Is there a better way?


Solution

  • I'd do your main template idea, or the dynamic template.

    dynamic template tends to be better when you have quite a few options that can be dynamically configured.

    But the main template I think ends up being more obvious when you only have a couple of choices.

    Either way can be converted easily to the other if you think you need the other option.