【问题标题】:MeteorJs: Return data Iron:routerMeteorJs: 返回数据 Iron:router
【发布时间】:2015-07-24 08:14:27
【问题描述】:

Iron 路由器返回数据在模板中,但我无法使用。

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

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

    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>
    

    【讨论】:

      猜你喜欢
      • 2015-09-15
      • 1970-01-01
      • 2016-03-18
      • 1970-01-01
      • 1970-01-01
      • 2013-07-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多