Meteor Iron Router : Passing data between routes
Asked Answered
D

2

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.

Dolli answered 6/9, 2013 at 7:58 Comment(0)
J
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.

Joleen answered 20/9, 2013 at 18:54 Comment(0)
T
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.

Towards answered 11/9, 2013 at 16:53 Comment(6)
I think Session is a good way in general to pass data around but in this case it's more of a syntax issue.Dolli
Session is not persisted between routes.Tilton
Sessions are globally persistent objects!Hypercriticism
@PeterStulinski Not in meteor, unless you add the persistant session package to meteor :)Handful
@Handful 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.Hypercriticism
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.Concert

© 2022 - 2024 — McMap. All rights reserved.