流星铁路由器:在路由之间传递数据 [英] Meteor Iron Router : Passing data between routes

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

问题描述

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



我在前端(客户端文件夹)上有一个javascript文件,它简单地调用Router.go()将post ID作为我的参数之一传递。



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



让我知道我是否应该发布更多信息。

FrontEnd.js

 模板.PostTiles.events({
//当选择选择
'click'.pin':function(event,template){
Router.go('Post',{_PostId:this .PostId});
}
});

post-detail.html

 < template name =PostDetail> 
< h3> {{问题}}< / p>
< / template>

Shared.js

  Router.map(function(){

this.route('Home',{
path:'/',
模板:'PostTiles',
data:{
//这里我们可以返回DB数据而不是将
//一个辅助方法附加到Template对象
QuestionsList:function (){
return POLL.find()。fetch();
}
}
});

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

});

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



我认为我已经缩小了问题的范围,仅仅能够渲染一个数据库条目,而不是使用 {{#each SomeList}}渲染它们的列表的语法。

解决方案

看起来像你找到答案/解决这个问题,但以防万一,我认为它是在您的 findOne 语句:

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

应为:

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



$ b $ p
$ b

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



希望有所帮助。


How do I pass data between two different routes and templates?

I have a javascript file on the front end (client folder) that simply calls Router.go() passing in the post ID as one of my parameters.

Below are the three main culprits (I believe). I've removed most of the code to make it easier to read. I can change to the PostDetail page with no problems. I can also retrieve the PostId on the PostDetail page from the Router. My problem is, the database entry (POLL) that is retrieved does not get rendered on the template. Hence {{Question}} is always blank even though the database entry is being returned.

Let me know if I should post more information.

FrontEnd.js

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

post-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'}
        }
    });

});

----- Update -----

I think I've narrowed down the issue to simply being able to render only one Database entry, instead of a list of them using the {{#each SomeList}} syntax.

解决方案

Looks like you found the answer / resolved this, but just in case, I think it's in your findOne statement:

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

should read:

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

(assuming that POLL has your posts listed by _id.

Hope that helps.

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

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