MeteorJS: Routing with Backbone.js

Problem

I'm trying to implement a router with BackboneJS in my MeteorJS app. When you call the url 'localhost:3000/1' my router stores the id '1' in the session. After that I want to take the id from the session and use it in my query to select an object from my collection. But whenever I try to use a session attribute in my query it fails. So I want to know if there is a better way for routing with MeteorJS and why my query fails.

test.js

Meteor.subscribe("test");

Test = new Meteor.Collection("test");

Session.set("id", null);

Template.hello.test = function () {
  var avg = 0, total = 0, cursor = Test.find(), count = cursor.count();
  cursor.forEach(function(e)
  {
    total += e.number;
  });
  avg = total / count;

  var session_id = Session.get("id");

  var test = Test.findOne({id: session_id}); //doesn't work
  if (test) {
    test.avg = avg;
  }

  return test;
}

//ROUTER
var TestRouter = Backbone.Router.extend({
  routes: {
    ":get_id":    "get_id" 
  },
  get_id: function (get_id) {
    Session.set("id", get_id);
    console.log(get_id);
  }
});

Router = new TestRouter;

Meteor.startup(function () {
  Backbone.history.start({pushState: true});
});

test.html

<head>
  <title>test</title>
</head>

<body>
  {{> hello}}
</body>

<template name="hello">
  <h1>Hello World!</h1>
  {{#if test}}
    {{#with test}}
      ID: {{id}}  Name: {{name}}  AVG: {{avg}}
    {{/with}}
  {{/if}}
</template>

model.js

Test = new Meteor.Collection("test");

Test.remove({});

if (Test.find().count() < 1) 
{
    Test.insert({id: 1,
                 name: "test1",
                 number: 13});

    Test.insert({id: 2,
                 name: "test2",
                 number: 75});
}

Meteor.publish('test', function () {
  return Test.find();
});
Problem courtesy of: fraherm

Solution

I debug the code and find out that 'id' in collection is an integer, while session_id is a string. You need parseInt to convert session_id.

I use page.js for routing, which is "Micro client-side router inspired by the Express router", an excellent work from "TJ Holowaychuk".

I strongly suggest it, since Meteor and backbone have some feature collisions in Model/Collection & View/Template.

Solution courtesy of: jifeng.yin

Discussion

Jifeng was right in the sense of if you only need routing capability then page.js is good enough.

Jifeng and I are in a same team. We had the conclusion of "Meteor and Backbone have some feature collisions in Model/Collection & View/Template" until recently. As our understanding on both Meteor and Backbone go deeper, that conclusion is in need of re-evaluation. Please refer to my last experiment code as BBCloneMail-on-Meteor: Derick Bailey's BBCloneMail modified to run on Meteor. The key is to implement a Backbone storage plugin to wire in Meteor's collection. Once the plugin wiring is in effect, there is only a little modification needed.

Discussion courtesy of: ZHANG Cheng

You might find iron router of interest - it's meteor specific and "knows about your subscriptions, data sources and helps you take care of common problems": https://github.com/EventedMind/iron-router

Discussion courtesy of: Jonathan Moffatt

This recipe can be found in it's original form on Stack Over Flow.