Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Meteor Iron Router : Passing data between routes

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.

like image 734
Free Lancer Avatar asked Sep 06 '13 07:09

Free Lancer


1 Answers

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.

like image 56
strack Avatar answered Oct 20 '22 04:10

strack