简体   繁体   中英

Relationship trouble - Mongo collections and MeteorJS

I am using Meteor, and trying to have one mongo collection that will contain products, and one that contains users.

Products have prices, but I also gave one product(as a test for now) a "dealerPrices" subcollection that contains an object like this:

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

My hope is to have a table on my site with a column that displays the 'partprice' and next to it another column that shows the price for the current logged in dealer. I could do a completely seperate collection for dealerPrices, but I am not sure which way is more efficient since I am new to Mongo.

My issue is targeting that number in the field with the "YH" or "AB" depending on the logged in user, the Users collection has a subcollection called "profile" that has a field called "code" that will match that "YH" or "AB" which is a unique code for each dealer.

I am using handlebars to display the data in Meteor, here is a bit of the html for displaying the table rows.

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>

I hope I am explaining this correctly, basically I am trying to find some alternative to joins and having a table for products, a table for the dealer-product-dealerPrice relationship and a user accounts table in a relational database.

You probably want to do this in a Template helper. First, create a template for each loop-through, instead of just using {{#each}} :

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

Then, add a Template helper for this new fooRow template:

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

Then just get rid of the stuff in the each loop, and replace it with:

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

That should do it!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM