meteor:如何列出一个集合(基础知识)

时间:2022-01-23 18:08:55

I really like what I saw with meteor. However, I get the impression with the documentation that you need to know the basics. So, something basic like listing a Collections is not described. Anyway, listing a collections is what I want (for now), so could someone help me with that ?

我真的很喜欢我用流星看到的东西。但是,我得到了您需要了解基础知识的文档的印象。因此,没有描述列出集合的基本内容。无论如何,列出一个集合是我想要的(现在),所以有人可以帮助我吗?

the js file:

js文件:

var Bars = new Meteor.Collection('bar'),

Bars.insert({ title: 'bar', index: 0})
Bars.insert({ title: 'foo', index: 1})

if (Meteor.isClient) {
    Meteor.subscribe('bar');

    var list = Bars.find({}).fetch();
}

if (Meteor.isServer) {
    Meteor.startup(function () {
       // code to run on server at startup
    });
}

the HTML file:

HTML文件:

<head>
    <title>Meteor test app</title>
</head>

<body>
    <ol class="bar">
        {{#each list}}
            <li>{{title}}</li>
        {{/each}}
   </ol>
</body>

Simple, but fundamental I thing. Also, would this update if someone else updates the bar collection ? Finally, is there a place where this meteor magic is explained starting with the basics ?

简单,但基本的东西。此外,如果其他人更新了酒吧集合,这会更新吗?最后,是否有一个地方从这个基础开始解释这个流星魔法?

1 个解决方案

#1


1  

The section of the documentation you're looking for is Template Helpers.

您正在寻找的文档部分是模板助手。

You need to assign the list variable you have to a helper that a template can see.

您需要将您拥有的列表变量分配给模板可以看到的帮助程序。

Something like this:

像这样的东西:

var Bars = new Meteor.Collection('bar');

if (Meteor.isClient) {
    Meteor.subscribe('bar');

    Template.bars.helpers({
        list: function () {
            return Bars.find({}).fetch();
        }
    });
}

if (Meteor.isServer) {
    Bars.insert({ title: 'bar', index: 0});
    Bars.insert({ title: 'foo', index: 1});
}

Then in HTML:

然后在HTML中:

<head>
    <title>Meteor test app</title>
</head>

<body>
    {{>bars}}
</body>
<template name="bars">
    <ol class="bar">
        {{#each list}}
            <li>{{title}}</li>
        {{/each}}
    </ol>
</template>

#1


1  

The section of the documentation you're looking for is Template Helpers.

您正在寻找的文档部分是模板助手。

You need to assign the list variable you have to a helper that a template can see.

您需要将您拥有的列表变量分配给模板可以看到的帮助程序。

Something like this:

像这样的东西:

var Bars = new Meteor.Collection('bar');

if (Meteor.isClient) {
    Meteor.subscribe('bar');

    Template.bars.helpers({
        list: function () {
            return Bars.find({}).fetch();
        }
    });
}

if (Meteor.isServer) {
    Bars.insert({ title: 'bar', index: 0});
    Bars.insert({ title: 'foo', index: 1});
}

Then in HTML:

然后在HTML中:

<head>
    <title>Meteor test app</title>
</head>

<body>
    {{>bars}}
</body>
<template name="bars">
    <ol class="bar">
        {{#each list}}
            <li>{{title}}</li>
        {{/each}}
    </ol>
</template>