【问题标题】:Meteor / Blaze rendering : {{each}} in another {{each}} doesn't return the right data (data context)Meteor / Blaze 渲染:另一个 {{each}} 中的 {{each}} 不返回正确的数据(数据上下文)
【发布时间】:2016-09-27 08:28:43
【问题描述】:

我想知道你能否给我一些线索来解决这个“问题”。我是 JS(+ Meteor)的新手,所以对你来说可能超级简单。我正在使用 helpersblaze/spacebars 进行渲染。

我有一个简单的助手:

Template.monitoring.helpers({

    'realtime': function(){
        return {
            house:    house.find( {} ),
            neighbours: neighbours.find( {} ),
        }
    } // end of realtime

}); // end of helpers

此时,一切正常。我能够检索我想要的数据。问题是我无法像我想要的那样渲染它。下面是我的template

<template name="monitoring">

   {{#each realtime.house}}
   <div class="card red">
     SHOW STUFF ABOUT HOUSE
   </div>

    <div class="card blue">
        {{#each realtime.neighbours}}
        SHOW STUFF ABOUT NEIGHBOURS OF THE SPECIFIC HOUSE
        {{/each}}
  </div>

  {{/each}} -> end of each realtime.house

</template>

更准确地说,当我的模板被渲染时:

  • 红牌数 = 房子数 => 完美
  • 每张红牌都包含一个房子的数据=>完美

  • 蓝牌数 = 房子数 => 完美

  • 每张蓝卡都包含所有邻居的数据 => 错误。我想要的是关于特定房子的邻居的数据

您知道如何在正确的地方获得正确的东西吗? 非常感谢。

【问题讨论】:

    标签: templates meteor meteor-blaze helpers


    【解决方案1】:

    您需要两个助手:一个返回房屋,另一个返回以房屋为上下文的邻居。如果没有架构,我无法给出准确的答案,但我们假设每个邻居都有一个 houseId 属性来连接两者。

    Template.monitoring.helpers({
      houses: function () {
        return house.find();
      },
    
      neighbors: function () {
        // Note the context here is a house because it's
        // called from within an each.
        return neighbours.find({ houseId: this._id });
      },
    });
    

    你的模板看起来像这样(假设每个邻居都应该是一张蓝卡):

    <template name="monitoring">
      {{#each houses}}
        <div class="card red">
          SHOW STUFF ABOUT HOUSE
        </div>
        {{#each neighbours}}
          <div class="card blue">
            SHOW STUFF ABOUT NEIGHBOURS OF THE SPECIFIC HOUSE
          </div>
        {{/each}}
      {{/each}}
    </template>
    

    【讨论】:

      猜你喜欢
      • 2016-03-10
      • 1970-01-01
      • 1970-01-01
      • 2018-08-26
      • 2015-01-25
      • 2016-05-29
      • 2016-04-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多