MeteorJS: Routing with Backbone.js

I am trying to implement a router with BackboneJS in my MeteorJS application. When you call the url "localhost: 3000/1", my router stores the identifier "1" in the session. After that, I want to take the identifier from the session and use it in my request to select an object from my collection. But whenever I try to use the session attribute in my request, it fails. So I want to know if there is a better way for routing with MeteorJS and why my request is not working.

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
javascript url-routing meteor
Jul 12 2018-12-12T00:
source share
3 answers

I am debugging the code and discover that the 'id' in the collection is an integer and session_id is a string. You need parseInt to convert session_id.

I use page.js for routing, which is a “Micro Client Express-inspired Inspector Router”, excellent work from “TJ Holowaychuk”.

I highly recommend this, as Meteor and the spine have some feature conflicts in Model / Collection and View / Template.

+3
Jul 13 2018-12-12T00:
source share

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

Jifeng and I are on the same team. We had the conclusion that "Meteor and Backbone have some collisions of objects in the model / collection and view / template" until recently. As our understanding of both Meteor and Beam goes deeper, this conclusion needs to be re-evaluated. Please refer to my latest experimental code as BBCloneMail-on-Meteor : Derick Bailey BBCloneMail modified to work on Meteor. The key is to implement the Backbone memory plugin to connect to the Meteor collection. After the plugin wiring is valid, only a small modification is required.

0
Aug 05 2018-12-12T00:
source share

You can find the iron router you are interested in - it is specific to the meteorite and "knows about your subscriptions, data sources and helps you take care of common problems": https://github.com/EventedMind/iron-router

0
Oct 10 '13 at 12:03
source share



All Articles