MeteorJs:Return 数据 Iron:router

MeteorJs: Return data Iron:router

Iron router return数据在模板中,但我无法使用它。

例如,我有工作数据库,其中每个工作都有一个 position(例如 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>

没有任何反应,我认为这是我的错,但我真的不明白如何解决这个问题。

有人可以帮忙吗?

您应该首先检查您的模板数据上下文中是否设置了正确的数据。以下是关于如何设置数据上下文以及如何从不同位置访问它的快速概括:

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>