我正在使用Meteor,并试图拥有一个包含产品的mongo集合和一个包含用户的mongo集合。

产品具有价格,但我还给了一种产品(作为目前的测试),它包含一个类似于以下对象的“ dealerPrices”子集合:

 "partprice" : "98",
    "dealerPrices" : {
        "YH" : "120",
        "AB" : "125"
    },


我希望在我的网站上有一个表,该表的一列显示“ partprice”,旁边是另一列,显示当前已登录经销商的价格。
我可以对DealerPrice进行完全独立的收集,但是由于我是Mongo的新手,因此我不确定哪种方法更有效。

我的问题是根据登录的用户使用“ YH”或“ AB”在字段中定位该数字,Users集合具有一个名为“ profile”的子集合,该子集合具有一个名为“ code”的字段,该字段将与该“ YH”匹配或“ AB”,这是每个经销商的唯一代码。

我正在使用把手在Meteor中显示数据,这是一些用于显示表行的html。

Larger code section:

<template name="products">

<h2> All Products <span class="productCount"></span></h2>

<table class="table table-condensed table-striped table-hover table-bordered">
  <thead>
    <tr>
      <th class="toHide">Unique ID</th>
      <th>Print</th>
      <th>Product</th>
      <th>FF Code</th>
      <th>Base Price</th>
      <th>My Price</th>
    </tr>
  </thead>

{{> AllProducts}}

</template>


<template name='AllProducts'>
   {{#each row}}
    <tr class='productRow'>
      <td class="product-id toHide">{{_id}}</td>
      <td class="print"><input type="checkbox" class="chkPrint"/></td>
      <td class="product-name">{{partname}}</td>
      <td class="product-code">{{code}}</td>
      <td class="product-az-price">${{partprice}}</td>
      <td class="product-dealer-price">${{dealerPrices.YH}}</td>
    </tr>
   {{/each}}
</template>


我希望我能正确地解释这一点,基本上,我正在尝试找到联接的替代方法,并在产品数据库中有一个产品表,一个Dealer-product-dealerPrice关系表和一个用户帐户表。

最佳答案

您可能想在模板帮助器中执行此操作。首先,为每个循环创建一个模板,而不仅仅是使用{{#each}}

<template name="fooRow">
    ... table rows
    <td class="product-dealer-price">{{userBasedThing}}</td>
</template>


然后,为此新的fooRow模板添加模板助手:

Template.fooRow.userBasedThing = function () {
    var result = "";
    if (Meteor.userId() && this.dealerPrices)
        result = this.dealerPrices[Meteor.user().profile[0].code];
    return result;
}


然后,只需删除each循环中的内容,然后将其替换为:

{{#each row}}
    {{> fooRow}}
{{/each}}


那应该做!

关于javascript - 关系麻烦-Mongo Collections和MeteorJS,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/18577011/

10-12 00:00
查看更多