关系问题-Mongo集合和MeteorJS
Relationship trouble - Mongo collections and MeteorJS
我正在使用Meteor,并试图拥有一个包含产品的mongo集合和一个包含用户的mongo集。
产品有价格,但我也给了一个产品(作为目前的测试)一个"dealerPrices"子集合,其中包含这样一个对象:
"partprice" : "98",
"dealerPrices" : {
"YH" : "120",
"AB" : "125"
},
我希望在我的网站上有一个表格,其中有一列显示"零件价格",旁边还有一列显示当前登录经销商的价格。我可以为经销商价格做一个完全独立的收集,但我不确定哪种方式更有效,因为我是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>
我希望我能正确地解释这一点,基本上我正在努力寻找一些替代联接的方法,并在关系数据库中拥有一个产品表、一个经销商-产品-经销商-价格关系表和一个用户帐户表。
您可能希望在Template助手中完成此操作。首先,为每个循环创建一个模板,而不是仅使用{{#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}}
那就行了!
相关文章:
- 如何使jQuery插件函数可调用以供独立使用,而不在集合上操作
- Meteor-将选定窗体中的对象添加到集合中
- MeteorJS:在带有回调的vzaar api上正确使用wrapAsync
- 主干集合重置和解析
- 使用数据上的角度更改设置集合的第一个元素的动画
- 为集合分配大量的模型弹药
- 如何使用backbone.js从集合中获取模型名称
- Lodash从集合创建集合
- 将当前用户的 ID 推送到 meteorjs 中集合/文档的内部数组
- MeteorJS:用户集合如何公开新字段
- 防止MeteorJS集合中的重复(通过表单)
- MeteorJS集合未定义
- 有没有办法让集合在 MeteorJS 上以块的形式发布数据
- 关系问题-Mongo集合和MeteorJS
- 如何在Meteorjs中从多个文件访问集合
- MeteorJS集合的奇怪行为
- 如果集合在MeteorJS中为null或不为null,则显示模板
- Meteorjs和mongodb:如何将多个集合组合成一个
- Meteorjs使用输入选择多个和推入集合
- MeteorJS在集合量很大时订阅使用情况