[英]meteorjs and mongodb: how to group many collections into one
My application is a list of products. 我的应用程序是产品列表。
I have too many, and want to group them, so I can refactor my publish/subscribe
code (and my templates as well). 我人数太多了,想对它们进行分组,因此我可以重构我的
publish/subscribe
代码(以及我的模板)。
Assume 4 distinct collections in my mongodb: balloons, balls, tents, tea.
假设我的mongodb中有4个不同的集合:
balloons, balls, tents, tea.
I need to group them into newly created foo
and bar
. 我需要将它们分为新创建的
foo
和bar
。 Then I can write two publish/subscribe statements instead of 4, and access my data by doing something like: 然后,我可以编写两个而不是4的publish / subscribe语句,并通过执行以下操作来访问我的数据:
on Client: 在客户端上:
Foo = new Meteor.Collection('foo');
Bar = new Meteor.Collection('bar');
in html template 在html模板中
{{#each foo.balloons }}
<p>{{ size }}</p>
<p>{{ price }}</p>
{{/each}}
or in another html template 或在另一个HTML模板中
{{#each bar.tents }}
<p>{{ size }}</p>
<p>{{ price }}</p>
{{/each}}
Personally I would not group them into multiple collections. 我个人不会将它们分为多个集合。 I would add a variable, such as "group" with the value "balloons", "balls", "tents" or "tea".
我将添加一个变量,例如“ group”,其值为“ balloons”,“ balls”,“ tents”或“ tea”。
When you then subscribe, you can opt to subscribe to one or more groups at the same time. 然后,当您订阅时,您可以选择同时订阅一个或多个组。 Then in your helpers, simply do something like:
然后在您的助手中,只需执行以下操作:
Template.foo.helpers({
balloons : function() {
return Foo.find({
"group" : "balloons"
});
},
tents : function() {
return Foo.find({
"group" : "tents"
});
}
});
Template.bar.helpers({
balls : function() {
return Foo.find({
"group" : "balls"
});
},
tea : function() {
return Foo.find({
"group" : "tea"
});
}
});
Update as per request: 根据要求更新:
<body>
{{> foo}}
{{> bar}}
</body>
<template name="foo">
<div id="balloons" class="product-list">
{{#each balloons}}
{{> productItem}}
{{/each}}
</div>
<div id="tents" class="product-list">
{{#each tents}}
{{> productItem}}
{{/each}}
</div>
</template>
<template name="bar">
<div id="balls" class="product-list">
{{#each balls}}
{{> productItem}}
{{/each}}
</div>
<div id="tea" class="product-list">
{{#each tea}}
{{> productItem}}
{{/each}}
</div>
</template>
<template name="productItem">
<div class="product">
<h1>{{title}}</h1>
<p>{{description}}</p>
</div>
</template>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.