铁路由器的流星数据上下文 [英] Meteor data-context with iron-router

查看:71
本文介绍了铁路由器的流星数据上下文的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Meteor的新手,我正在尝试在显示一段文字的页面中设置数据上下文.我需要访问Passion_item.js Template.passageItem.rendered中的数据,但此时未设置任何上下文.我认为我需要类似{{#with Passage}}的东西,但是one_passage.html中不存在段落".

I am new to Meteor and I'm trying to set the data context in a page that displays one passage. I need to access the data in passage_item.js Template.passageItem.rendered but no context is set at that point. I think I need something like {{#with passage}} but "passage" does not exist in one_passage.html.

以下是一些代码段.谢谢.

Here are some code snippets. Thanks.

router.js

router.js

Router.map(function() {
    this.route('passagesList', {path: '/'});
    this.route('onePassage', { 
    path: '/passages/:_id',
    data: function() { return Passages.findOne(this.params._id); }
    });
});

one_passage.html

one_passage.html

<template name="onePassage">
    {{> passageItem}}
</template>

passage-item.html

passage-item.html

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

passage_item.js

passage_item.js

Template.passageItem.helpers({
});

Template.passageItem.rendered = function() {
    Meteor.defer(function() {
    $('.passage-content').lettering('words');
   //I want to be able to access the data object here. I have a list of words that are highlighted
    });
};

推荐答案

集合

假设您以这种方式创建了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
};

这篇关于铁路由器的流星数据上下文的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆