作为序言:我是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>
蔬菜的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);
}
});
所以-我需要知道是否有一种方法可以完成此任务。我可以将所有成分硬编码为布尔变量,然后根据布尔值的正确性来显示可能的配方,但这似乎很耗时,我宁愿创建一个动态系统,在其中添加蔬菜/成分和可以轻松添加或减少食谱。
最佳答案
要创建客户端集合,只需不给它一个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);
关于javascript - 如何在 meteor 中进行临时收集?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/28246879/