MeteorJS: Routing with Backbone.js

2019-05-10 00:25发布

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();
});

3条回答
Emotional °昔
2楼-- · 2019-05-10 00:40

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

查看更多
甜甜的少女心
3楼-- · 2019-05-10 01:05

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.

查看更多
混吃等死
4楼-- · 2019-05-10 01:06

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.

查看更多
登录 后发表回答