【问题标题】:using data between templates in meteor.js在meteor.js中使用模板之间的数据
【发布时间】:2015-11-04 18:40:12
【问题描述】:

我是meteor.js 的新手,我正在练习构建一个简单的小应用程序。我的问题是如何在模板里面使用从mongodb获取的模板数据。这是结构:

路由器.js

Router.route('/point/:_id', {name: 'pointDetail', data: function() {
    return InterPoints.findOne(this.params._id);
} });

模板pointDetail.html

<template name="pointDetail">
 <some tags>
   {{datathree}}
 </some tags>
   {{> map}}
</template>

模板图.html

<template name="map">
  <div class="map-container">
    {{> googleMap name="exampleMap" options=exampleMapOptions}}
  </div>
</template>

map.js

  Template.map.helpers({
    exampleMapOptions: function() {
      // Make sure the maps API has loaded
      if (GoogleMaps.loaded()) {
        // Map initialization options
        return {
          center: new google.maps.LatLng(coordX, coordY),
          zoom: 8
        };
      }
    }
  });

我想使用从路由器检索到的数据在 map.js 中填充 coorsx 和 coordy。 像 coordx = {{coordsx}} 有可能吗?

非常感谢。

【问题讨论】:

标签: meteor


【解决方案1】:

您可以通过Blaze.currentView.parentView.templateInstance() 检索父模板的数据上下文。假设您的Interpoints 文档具有属性coordxcoordy

Template.map.helpers({
  exampleMapOptions: function() {
    // Make sure the maps API has loaded
    if (GoogleMaps.loaded()) {
      // Map initialization options
      var parentTemplate = Blaze.currentView.parentView.templateInstance();
      var coordX = parentTemplate.data.coordx;
      var coordY = parentTemplate.data.coordy;
      return {
        center: new google.maps.LatLng(coordX, coordY),
        zoom: 8
      };
    }
  }
});

【讨论】:

  • 感谢 BraveKenny,我试了一下,但没有用。我的 Interpoints 文档具有语言、名称、类型、区域、coordx、coordy 等属性。如果我使用你的代码,它会抛出一个错误: Exception in template helper: TypeError: Cannot read property 'coordx' of undefined 。我尝试了不同的级别,但仍然相同。我将代码放在 detailPoints.js 中,带有 console.log 但同样的错误。谢谢!
  • 嗯,我想我错了,there is a difference between nested data contexts and nested templates。我用从链接问题中收集到的信息编辑了我的答案。
  • 感谢 BraveKenny,现在它可以完美运行了! Rishat Muhametshin 解决方案也可以正常工作。非常感谢您的帮助,问候!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-01-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多