gpt4 book ai didi

javascript - Meteor 模板助手和对集合字段的访问

转载 作者:行者123 更新时间:2023-12-02 15:26:36 27 4
gpt4 key购买 nike

我有两个集合:

Group = {
users: [Array_of_User]
}

User = {
name: _string_
}

我正在列出组,并试图在模板中了解用户是否在组中:

mytemplate.js

Template.mytemplate.helpers({
groups: function(){
return Groups.find();
},
currentUsername: 'test'
});

mytemplate.html

<template name="main">
<ul>
{{#each groups}}
<li>
{{#if [the group contains currentUsername] }}
contains
{{else}}
doesn't contain
{{/if}}
</li>
{{/each}}
</ul>
</template>

问题是:我可以在助手上放什么来代替 [the group contains currentUsername]让它发挥作用?

另外,我并不是说这就是这样做的方法。我愿意接受任何建议,即使这意味着我必须做出很多改变。

最佳答案

您可以使用Underscore.js功能_.findWhere(list, properties)检查组中是否包含用户名:

if (Meteor.isClient) {
Template.main.helpers({
groups: function() {
return Groups.find();
},
currentUsername: 'Matthias',
isInGroup: function(username) {
return !!_.findWhere(this.users, {
name: username
});
}
});
}
<小时/>
<template name="main">
<ul>
{{#each groups}}
<li>
{{#if isInGroup currentUsername}}
contains
{{else}}
doesn't contain
{{/if}}
</li>
{{/each}}
</ul>
</template>
<小时/>
if (Meteor.isServer) {
Meteor.startup(function() {
Groups.insert({
users: [{
name: "Matthias"
}, {
name: "Angie"
}]
});
});
}

这是一个MeteorPad .

关于javascript - Meteor 模板助手和对集合字段的访问,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33655385/

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