12

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.

Free Lancer
  • 992
  • 2
  • 15
  • 32

2 Answers2

2

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.

strack
  • 574
  • 4
  • 12
0

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.

Will Kessler
  • 525
  • 6
  • 17
  • I think Session is a good way in general to pass data around but in this case it's more of a syntax issue. – Free Lancer Sep 24 '13 at 00:45
  • Session is not persisted between routes. – Joseph Jul 09 '14 at 11:17
  • 3
    Sessions are globally persistent objects! – Piotr Stulinski Mar 24 '15 at 10:52
  • @PeterStulinski Not in meteor, unless you add the persistant session package to meteor :) – Pwnball May 20 '15 at 09:43
  • @Pwnball session is persisted between routes its not persistent between page refreshes (hit refresh on browser)... if thats what you want you need the package which uses local storage to reload them when you do hit the refresh. Iron router does not cause "page refresh" when navigating hence you do not need the package. – Piotr Stulinski May 20 '15 at 17:03
  • Now anyway, Iron Router causes a page refresh after this.next() is called inside a route function, so Meteor's Session data on the client is lost. – Eric Majerus Aug 25 '15 at 00:01