Iron router: how to establish a session using this.params._id

I have a route / scoreboard / 1, where 1 is not a calm identifier of the object, but the identifier of the court, which I want to use to select the games where the court is.

I tried this code that establishes a session but does not load the template. Usually the template is loaded if I delete the event and the var code hard drive.

this.route('scoreboard',
    {

    path: '/scoreboard/:_id',

    template: 'scoreboard',

    onBeforeAction: function () {
       Session.set('court', this.params._id);
    }

  }); //route

I found this to work. What doesn't work is:

var court = Session.get("court");

console.log(court); -> 1

myGame = Games.findOne({court_id: court});

while it works:

myGame = Games.findOne({court_id: 1});

found him!

  var court = parseInt(Session.get('court'));
+4
source share
2 answers

This works for me:

$ meteor create test
$ cd test
$ meteor add iron:router

test.js:

if (Meteor.isClient) {
    Template.scoreboard.id = function() {
        return Session.get('court');
    }
}

Router.route('/scoreboard/:_id', {
    name: 'scoreboard',
    path: '/scoreboard/:_id',
    template: 'scoreboard',
    onBeforeAction: function () {
       Session.set('court', this.params._id);
    }
});

test.html:

<head>
</head>

<template name="scoreboard">
  Scoreboard<br/>
  id is: {{id}}
</template>

Then go to localhost:3000/scoreboard/123.

+8
source

, . Robomongo ( ) .

+2

All Articles