Javascript 如何在meteor中进行临时收集?
作为前言:我对Meteor很陌生 我的目标是:我试图建立一个网站,将蔬菜作为一个集合来存储,将食谱(包含所需的成分)作为一个集合来存储,并将根据用户是否已选中的成分向用户显示食谱列表。仅显示包含选中标记成分的配方 为此,我想创建一个临时集合,其中包含选中标记的成分,并使用它来确定要显示的食谱。我尝试使用会话,但我不知道是否可以从会话中插入/删除和查找项目,因此我更喜欢使用集合。但是,如果多个用户同时使用该网站,我不希望集合受到干扰,因此我希望集合对每个访问者都是唯一的(客户端和临时的,如sesson) 到目前为止,我有以下代码: 显示蔬菜的模板:Javascript 如何在meteor中进行临时收集?,javascript,mongodb,meteor,spacebars,Javascript,Mongodb,Meteor,Spacebars,作为前言:我对Meteor很陌生 我的目标是:我试图建立一个网站,将蔬菜作为一个集合来存储,将食谱(包含所需的成分)作为一个集合来存储,并将根据用户是否已选中的成分向用户显示食谱列表。仅显示包含选中标记成分的配方 为此,我想创建一个临时集合,其中包含选中标记的成分,并使用它来确定要显示的食谱。我尝试使用会话,但我不知道是否可以从会话中插入/删除和查找项目,因此我更喜欢使用集合。但是,如果多个用户同时使用该网站,我不希望集合受到干扰,因此我希望集合对每个访问者都是唯一的(客户端和临时的,如sess
<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>
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);
}
});
所以,我需要知道是否有办法完成这项任务。我可以将所有成分硬编码为布尔变量,然后根据布尔值显示可能的配方,但是这似乎很耗时,我更愿意创建一个动态系统,在这个系统中,蔬菜/配料和食谱的列表可以很容易地添加到/减去。要创建客户端集合,只需不给它一个name参数
// 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);
您可以使用仅客户端集合。阅读我的文章了解详细信息:谢谢您的解释。现在我的第二个问题是:是否可以将从一个集合复制的文档插入到另一个集合中?您的意思是:
Collection1.insert(Collection2.findOne())代码>我创建了一个名为CheckedVeggies的客户端集合:CheckedVeggies=new Mongo.Collection('null');Template.vegies.events({“click.toggle checked”:函数(){this.\u id,{$set:{checked:!this.checked}};var ItemID=this.\u id;checkedvegies.insert(vegets.find({vegetar:ItemID}).fetch();});这似乎不太可能working@SamLouisMilligancollection.insert方法需要一个文档。您需要使用遍历游标,并在游标中插入每个文档,而不是获取。