Meteor Iron Router : Passing data between routes

杀马特。学长 韩版系。学妹 提交于 2019-12-20 18:46:31

问题


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.


回答1:


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.




回答2:


Could you pass the info in the Session? the docs for that are here http://docs.meteor.com/#session. That's what I'm planning on doing.



来源:https://stackoverflow.com/questions/18652743/meteor-iron-router-passing-data-between-routes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!