How do I activate an Iron Router route without cha

2019-06-07 22:22发布

问题:

In my Meteor app, I'm trying to pass data from my Iron Router controller to a template that's a partial in my app. How do I activate a route without changing the path? I don't want to switch pages, only render the partial with my data (if that makes sense).

Here's how the app works:

Right now, the route is activated when I click a dropdown link in my navbar

Template.navbar.events
  'click #threads-link': (event)->
    Router.go 'allThreads'

This renders the template with my data as long as I have a path in my route. But since I don't want the path to change, I tried leaving the path--and then the template doesn't retrieve the data from my controller!

Router.map ->
  @route "allThreads",
    controller: ThreadsController 

Do you know how to get my template partial to access the data in the controller without changing the path? Thanks in advance!

回答1:

First, you're probably seeing some issues because the latest version of Iron Router requires you to be on Meteor v0.8.0. If you're on a previous version of Meteor you'll need to use the older Iron Router (0.6).

To customize rendering inside of your ThreadsController you can just override the action function, or you can define a hook. Then, you can call the render method to render whichever templates you'd like. Here is an example:

ThreadsController = RouteController.extend({
  action: function () {
    // Example 1. render this controller's default template and all yields
    this.render();

    // Example 2. render myTemplate into the main yield
    this.render('myTemplate');

    // Example 3. render templates into yield regions
    this.render('myFooter', {to: 'footer'});
  }
});