1

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!

Stu Stein
  • 888
  • 7
  • 6
  • does `this.render('templateName', {to: 'region'})` not work for you? (https://github.com/EventedMind/iron-router/blob/master/DOCS.md#custom-rendering) – Christian Fritz Mar 31 '14 at 23:16
  • Thanks so much for the pointer, @ChristianFritz. I wasn't using yields before, I was using partials. I'm trying to change over now, and I'm getting a subsequent error: "Parse error: {{> yield region='navRegion'}}". Have you ever run into that by any chance? Really appreciate the help. – Stu Stein Mar 31 '14 at 23:50
  • Looks like `{{yield 'navRegion'}}` renders, but `{{> yield region='navRegion'}`} does not! Also, `this.render('templateName', {to: 'region'})` is still only rendering the template and it's not accessing/activating the data from the controller. Any other thoughts by any chance? Thanks again! – Stu Stein Apr 01 '14 at 00:23

1 Answers1

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'});
  }
}); 
cmather
  • 1,950
  • 14
  • 17
  • Thanks @cmather! Quick clarification: I actually have two controllers on one layout: NotesController is supposed to be for the main yield, while ThreadsController is for a navbar (which is global to the app). Right now, NotesController passes its data to the template when the '/notes' path is activated, but how can I get ThreadsController to pass its data to the nav at a global level at the same time? Is it possible, or did I take a wrong turn? Sorry for the confusion. – Stu Stein Apr 01 '14 at 06:29
  • There is a one-to-one between a route and a route controller. You'll only ever have one controller running at a time, based on the route you're on. If you want to set a global value from a controller, and it doesn't make sense to do it in the global data context, you can always set a session value. For example: Session.set('currentId', this.params._id); – cmather Apr 03 '14 at 22:13
  • Thanks @cmather! That makes sense and answers my question perfectly! – Stu Stein Apr 03 '14 at 23:59