且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

铁路由器的流星数据上下文

更新时间:2022-06-25 08:25:40

集合

假设您以这种方式创建了Passages集合,并且您已经启用了自动发布(默认情况下为自动发布):

Assuming you created your Passages collection like this and you've got autopublish turned on (which it is by default):

Passages = new Meteor.Collection('passages');

路由器地图

您这样映射路由器:

Router.map(function() {
    this.route('onePassage', { 
        path: '/passages/:_id',
        template: 'passageItem' // <-- to be explicit
        data: function() {
            return Passages.findOne(this.params._id);
        }
    });
});

模板

您的模板看起来像下面的模板:

And your template looks like the template below:

<template name="passageItem">
    <div class="passage">
        <div class="one-passage">
            <h4><a href= "{{pathFor 'onePassage'}}">{{title}}</a></h4>
            <div class="passage-content">
                {{content}}
            </div>
        </div>
    </div>
</template>

模板中'this'的范围将设置为Passages.findOne选择器返回的文档.

The scope of 'this' in the template will be set to document returned by the Passages.findOne selector.

如果模板不呈现,则意味着您正在搜索不存在的段落,或者您的段落缺少标题或内容字段.

If the template doesn't render that means you're either searching for passage that doesn't exist, or your passage is missing title or content fields.

渲染功能

现在是您问题的最后一部分.呈现函数中"this"的范围设置为模板实例.因此,如果您需要访问模板数据,请尝试以下操作:

Now for the last part of your question. The scope of 'this' in a rendered function is set to the template instance. So if you need to access the template data try this:

Template.passageItem.rendered = function() {
    console.log(this.data); // you should see your passage object in the console
};