繁体   English   中英

meteorjs和mongodb:如何将多个集合分组为一个

[英]meteorjs and mongodb: how to group many collections into one

我的应用程序是产品列表。

我人数太多了,想对它们进行分组,因此我可以重构我的publish/subscribe代码(以及我的模板)。

假设我的mongodb中有4个不同的集合: balloons, balls, tents, tea.

我需要将它们分为新创建的foobar 然后,我可以编写两个而不是4的publish / subscribe语句,并通过执行以下操作来访问我的数据:

在客户端上:

Foo = new Meteor.Collection('foo');
Bar = new Meteor.Collection('bar');

在html模板中

{{#each foo.balloons }}

    <p>{{ size }}</p>  
    <p>{{ price }}</p>

{{/each}}

或在另一个HTML模板中

{{#each bar.tents }}

    <p>{{ size }}</p>  
    <p>{{ price }}</p>

{{/each}}

我个人不会将它们分为多个集合。 我将添加一个变量,例如“ group”,其值为“ balloons”,“ balls”,“ tents”或“ tea”。

然后,当您订阅时,您可以选择同时订阅一个或多个组。 然后在您的助手中,只需执行以下操作:

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

根据要求更新:

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM