1
votes

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?

1
the best you have come up with looks reasonable to me. Is something not working as you like with this approach? - JeremyK
Thanks, I implemented both and settled on the dynamic template. It felt like the wrong place for it but no big deal. I was concerned about performance loading all three templates at the same time, but this seems like how we do it :-) - Michael Cole
How much performance difference was there between the two implementations? Given that only one if statement will be true, you still only load and render one template. I wouldn't expect any significant performance difference between the two. - JeremyK
@JeremyK they seem the same, but I'm not stressing them for performance. - Michael Cole

1 Answers

1
votes

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.