title: meteor iron-router nested routes
body: <p>I have two meteor collections with a one-to-many relationship: buildings and spaces</p>
  
  <p>On my building page, I want to show the spaces related to the building.</p>
  
  <p>For now, I did it this way:</p>
  
  <pre><code>buildingsRoute.coffee
      BuildingController = RouteController.extend(template: "buildings")
      Router.map -&gt;
        @route "building",
          path: "/buildings/:_id"
          waitOn: -&gt;
            subs.subscribe "allBuildings"
            subs.subscribe "allSpaces"
  
          data: -&gt;
            building: Buildings.findOne(@params._id)
            spaces: Spaces.find({building_id: @params._id})
  </code></pre>
  
  <p>building.jade:</p>
  
  <pre><code>template(name="building")
          +with building
              .building
                  .page-header.position-relative
                      h1.inline #{name}
                  .row
                      .col-xs-12
                         +spacesList
  
  template(name="spacesList")
      .widgets
          .row
              h1 Spaces
              +each spaces
                  .col-xs-12.col-sm-6
                      .widget-box.widget-color-purple
                          .widget-header
                              a(href="{{pathFor 'space'}}") #{name}
                          .widget-body
                              p Content here
  </code></pre>
  
  <p>This doesn't work as such, I guess because the data context of the spacesList template is not the same as the one defined by iron router for building.</p>
  
  <p>I could replace "+each spaces" by "+each ../spaces" but this doesn't seem like a very generic solution to me (what if I want to use my spaceslist template in another context?)</p>
  
  <p>So I tried to define the data context in template helpers like this:</p>
  
  <pre><code>Template.spacesList.helpers
    spaces: Spaces.find({building_id: @params._id})
  </code></pre>
  
  <p>But I get the error message:</p>
  
  <pre><code>Spaces is not defined.
  </code></pre>
  
  <p>So I'm a bit confused. What is the meteor way to implement nested routes properly?</p>
  
  <p>Thank you!</p>
  
  <p>EDIT:</p>
  
  <p>Definition of Spaces collection: /models/space.coffee</p>
  
  <pre><code>@Spaces = new Meteor.Collection("spaces",
    schema:
      building_id:
        type: String
        label: "building_id"
        max: 50
  
      name:
        type: String
        label: "Name"
        optional: true
        max: 50
  
      creation_date:
        type: Date
        label: "Creation date"
        defaultValue: new Date()
  )
  </code></pre>
  
  <p>Publications: /server/publications.coffee</p>
  
  <pre><code># Buildings
  
      Meteor.publish "allBuildings", -&gt;
        Buildings.find()
  
      Meteor.publish "todayBuildings", -&gt;
        Buildings.find creation_date:
          $gte: moment().startOf("day").toDate()
          $lt: moment().add("days", 1).toDate()
  
  
  
      # Publish a single item
      Meteor.publish "singleBuilding", (id) -&gt;
        Buildings.find id
  
  
      # Spaces
      # Publish all items
      Meteor.publish "allSpaces", -&gt;
        Spaces.find()
  </code></pre>
  
  <p>EDIT 2</p>
  
  <p>After some researches, I finally came up with a solution:</p>
  
  <pre><code>Template.spacesList.helpers
    spaces: () -&gt;
      if Router._currentController.params._id
        subs.subscribe "buildingSpaces", Router._currentController.params._id
        Spaces.find()
      else
        subs.subscribe "allBuildings"
        Spaces.find()
    nbr_spaces: () -&gt;
      Spaces.find().count()
  </code></pre>
  
  <p>With an additional publication:</p>
  
  <pre><code># Publish all items for building
  Meteor.publish "buildingSpaces", (building_id) -&gt;
    Spaces.find({building_id: building_id})
  </code></pre>
  
  <p>The errors were:</p>
  
  <ul>
  <li>the fact that the space definition wasn't wrapped into a function </li>
  <li>the @params._id that I replaced by the not very sexy Router._currentController.params._id but I couldn't find any shortcut for this.</li>
  </ul>
  
  <p>I still don't know whether this is the Meteor (best) way to manage nested routes...</p>
  
  <p>Any better recommendation?</p>