流星:返回数据Iron:router

问题描述 投票:0回答:1

铁路由器返回数据在模板中,但我不能使用它。

例如,我有带有作业的数据库,其中每个作业都有一个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>

没事,我认为这是我的错,但我真的不明白如何解决此问题。

有人可以帮忙吗?

meteor iron-router
1个回答
0
投票

您首先应该检查是否在模板数据上下文中设置了正确的数据。 以下是有关如何设置数据上下文以及如何从不同位置访问数据上下文的简要概述:

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>
© www.soinside.com 2019 - 2024. All rights reserved.