如何在流星中进行临时收集?

Sam*_*gan 1 javascript mongodb meteor spacebars

作为序言:我对Meteor很新.

我的目标:我正在尝试建立一个将蔬菜存储为集合的网站,将食谱(包含所需的成分)存储为一个集合,并根据用户是否有成分向用户显示食谱列表已勾选.仅显示包含选中标记成分的配方.

为此,我想创建一个临时集合,其中包含带有复选标记的成分,并使用它来识别要显示的配方.我尝试使用会话,但我不知道是否可以插入/删除并从会话中查找项目,所以我更喜欢使用集合.但是,如果多个用户同时使用该网站,我不希望该集合收到干扰,所以我希望该集合对每个访问者来说都是唯一的(客户端和临时用户,如sesson)

到目前为止,我有以下代码:

显示蔬菜的模板:

<template name="veggies">
  <ul>
    {{#each vegetables}}
      <li class="{{#if checked}}checked{{/if}}">
        <input type="checkbox" checked="{{checked}}" class="toggle-checked" />
        <span class="text">{{vegetable}}</span>
      </li>
    {{/each}}
  </ul>
</template>

<template name="button">
  <button class="submit">Sugest recipes</button>
</template>
Run Code Online (Sandbox Code Playgroud)

蔬菜的Javascript文件代码:

Recipes = new Mongo.Collection('recipes');
vegetables = new Mongo.Collection('vegetable');


if (Meteor.isClient) {


// VEGGIES

  // this displays the vegetables
  Template.veggies.helpers({
    'vegetables': function() {
      return vegetables.find()
    }

  });

  // this selects toggles the check mark
  Template.veggies.events({
    "click .toggle-checked": function () {
      this._id, {$set: {checked: ! this.checked}};
      var ItemID = this._id
      Session.set('selectedVeggies', ItemID);
    },
  });

  // this submits the request and reveals possible recipes
  Template.button.events({
    "click .submit": function () {
      Session.set('PossibleRecipes', true);
    }
  });
Run Code Online (Sandbox Code Playgroud)

所以 - 我需要知道是否有办法完成这项任务.我可以将所有成分硬编码为布尔变量,然后根据哪个布尔值为真来显示可能的配方,但这似乎很耗时,我宁愿创建一个动态系统,其中包含蔬菜/配料列表食谱可以轻松添加/减少.

sbk*_*ing 6

要创建客户端集合,请不要为其指定名称参数.

// This collection has a name, and will work on both the server and client
Recipes = new Mongo.Collection('recipes');

// This collection is client-only, and does not have a name.
// It will not be synchronized with the server.
Vegetables = new Mongo.Collection();

// To be more explicit, you can use `null` for the name:
Meats = new Mongo.Collection(null);
Run Code Online (Sandbox Code Playgroud)