【问题标题】:Relationship trouble - Mongo collections and MeteorJS关系问题 - Mongo 集合和 MeteorJS
【发布时间】:2013-09-03 03:42:59
【问题描述】:

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

产品有价格,但我也给了一个产品(作为现在的测试)一个“dealerPrices”子集合,其中包含这样的对象:

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

我希望在我的网站上有一个表格,其中有一列显示“partprice”,旁边的另一列显示当前登录经销商的价格。 我可以为dealerPrices 做一个完全独立的收集,但我不确定哪种方式更有效,因为我是Mongo 的新手。

我的问题是根据登录用户在“YH”或“AB”字段中定位该数字,用户集合有一个名为“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>

我希望我能正确解释这一点,基本上我试图找到一些替代联接,并在关系数据库中拥有一个产品表、一个经销商-产品-dealerPrice 关系表和一个用户帐户表。

【问题讨论】:

    标签: javascript meteor handlebars.js


    【解决方案1】:

    您可能希望在模板助手中执行此操作。首先,为每个循环创建一个模板,而不是只使用{{#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}}
    

    应该这样做!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-02-08
      • 1970-01-01
      • 2018-07-15
      • 2014-04-14
      • 2017-10-12
      • 1970-01-01
      • 2015-11-18
      • 2020-03-02
      相关资源
      最近更新 更多