gpt4 book ai didi

javascript - 如何在 meteor 中进行临时收藏?

转载 作者:行者123 更新时间:2023-11-30 12:29:15 25 4
gpt4 key购买 nike

作为序言:我是 Meteor 的新手。

我的目标:我正在尝试制作一个网站,将蔬菜存储为一个集合,将食谱(包含所需的成分)存储为一个集合,并根据用户是否有用户勾选的成分。只会显示包含勾选成分的食谱。

为此,我想创建一个包含已勾选成分的临时集合,并使用它来确定要显示的食谱。我尝试使用 session ,但我不知道是否可以从 session 中插入/删除和查找项目,所以我更愿意使用集合。但是,如果多个用户同时使用该网站,我不希望集合受到干扰,所以我希望集合对每个访问者都是唯一的(客户端和临时的,如类(class))

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

蔬菜展示模板:

<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);
}
});

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

最佳答案

要创建一个客户端集合,不要给它一个名称参数。

// 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com