流星铁路由器:在路由之间传递数据

时间:2013-09-06 07:58:44

标签: javascript meteor handlebars.js iron-router

如何在两个不同的路径和模板之间传递数据?

我在前端(客户端文件夹)上有一个javascript文件,它只是调用Router.go()传递帖子ID作为我的参数之一。

以下是三个主要罪魁祸首(我相信)。我删除了大部分代码,以便于阅读。我可以毫无问题地更改为 PostDetail 页面。我还可以从路由器上检索 PostDetail 页面上的 PostId 。我的问题是,检索到的数据库条目( POLL )不会在模板上呈现。因此,即使正在返回数据库条目, {{Question}} 也始终为空。

如果我发布更多信息,请告诉我。

FrontEnd.js

Template.PostTiles.events({
  // When a choice is selected
  'click .pin' : function(event, template) {        
    Router.go('Post', {_PostId: this.PostId});    
  }
});

发表-detail.html

<template name="PostDetail">
    <h3>{{Question}}</p>
</template>

Shared.js

Router.map( function() {

    this.route('Home', {
        path: '/',
        template: 'PostTiles',
        data: {
            // Here we can return DB data instead of attaching 
            // a helper method to the Template object
            QuestionsList: function() {
                return POLL.find().fetch();
            }           
        }
    });

    this.route('Post', {
        template: 'PostDetail',
        path: '/Post/:_PostId',
        data: function() {          
            return POLL.findOne(this.params._PostId); 
        },
        renderTemplates: {
            'disqus': {to: 'comments'}
        }
    });

});

-----更新-----

我认为我已经将问题缩小到只能使用 {{#each SomeList}} 语法只呈现一个数据库条目而不是它们的列表。

2 个答案:

答案 0 :(得分:2)

看起来你找到了答案/解决了这个问题,但为了以防万一,我认为这是在你的findOne陈述中:

data: function() {          
        return POLL.findOne(this.params._PostId); 
    },

应为:

data: function() {          
        return POLL.findOne({_id:this.params._PostId}); 
    },

(假设POLL的帖子由_id列出。

希望有所帮助。

答案 1 :(得分:0)

你能在会话中传递信息吗?这方面的文档在http://docs.meteor.com/#session。这就是我打算做的事情。

相关问题