2
votes

I'm reading the documentation at https://github.com/EventedMind/iron-router/blob/devel/Guide.md and I can't get a clear picture on when I would pass a function to the route() method and when I would pass an object.

Example for rendering a template with data -- passes a function as the second parameter:

Router.route('/post/:_id', function () {
  this.render('Post', {
    data: function () {
      return Posts.findOne({_id: this.params._id});
    }
  });
});

Example for waiting for a subscription to complete -- passes an object as the second parameter:

Router.route('/post/:_id', {
  // this template will be rendered until the subscriptions are ready
  loadingTemplate: 'loading',

  waitOn: function () {
    // return one handle, a function, or an array
    return Meteor.subscribe('post', this.params._id);
  },

  action: function () {
    this.render('myTemplate');
  }
});

As you can see, some examples in the documentation use one form and some use the other. Generally speaking, when I find some documentation that addresses what I have in mind, it uses a different form than the one I have adopted from following an example elsewhere in the documentation.

What is the relationship between the two forms? How do I mix and match?

1

1 Answers

1
votes

I am currently learning of this myself. I have found that you can alternately do both methods as you mentioned, depending upon your needs.

The format is as you mentioned, that you can pass a function as the second argument for the route definition.

The biggest difference I have found is that using a function can override some settings in Router.config. It seems like for instance, you must be careful of how you define renderings, as to not override layout templates.

I feel like it's a matter of preference as I have seen many different examples of how to set up routes (for eg. Iron router and hooks).

Basically I am using Router.map() to try and handle most standard settings, and then looking to define hooks to control flow through the routers. Then I am looking to set up custom controllers as needed, and as the project scales out.

All in all, I feel myself drawn away from using a function in defining a route, since it requires more explicitness, although I can see it having a purpose, when wanting to more finely process routes, conditions, and parameters.