Search code examples
meteoriron-router

MeteorJs: Return data Iron:router


Iron router return data is in template but I can't use it.

For example I have db with jobs, where every job has a position (e.g. jobs.position):

ExistJobPostController = RouteController.extend({
  layoutTemplate: 'existJob',
  data:function() {return Posts.findOne(this.params._id); }
})
Router.map(function() {
  this.route('existJob', {    
    path: '/jobs/:_id',
    controller: ExistJobPostController,
  });
});
<template name="existJob">      
  {{position}}
</template>

And nothing happens, I think that it's my fault, but I really can't understand how to fix this.

Can anybody help?


Solution

  • You should first check that the correct data is even being set on your template data context. Here's a quick general summary of how to set the data context and how to access it from various locations:

    Router.map(function() {
      this.route('index', {    
        path: '/index',
        data: function(){
          var obj = {
            fname: "Tom",
            lname: "Smith"
          };
          return obj;
        }
      });
    });
    
    Template.index.onRendered(function(){
      console.log(this.data.fname);
    });
    
    Template.index.events({
      'click body': function(e, tmpl){
        console.log(tmpl.data.fname);
      }
    });
    
    Template.index.helpers({
      lastName: function(){
        return this.lname;
      }
    });
    
    <template name="index">
    
      You have to use `this` when directly accessing template data from spacebars.
      {{this.firstName}}
    
      The following comes from Template.index.helpers:
      {{lastName}}
    
    </template>