MeteorJS:Backbone.js的与路由 [英] MeteorJS: Routing with Backbone.js
问题描述
我想实现在我MeteorJS应用BackboneJS路由器。
当你调用URL的'localhost:1分之3000我的路由器存储ID'1'的会话。从那以后,我想从会议采取ID,并用它在我的查询,以便从我的收藏中选择的对象。但每当我试图在我的查询中使用一个会话属性失败。所以我想知道是否有与MeteorJS,为什么我的查询失败,路由更好的办法。
test.js
Meteor.subscribe(测试);测试=新Meteor.Collection(测试);Session.set(ID,NULL);Template.hello.test =功能(){
VAR平均= 0,总= 0,光标= Test.find(),计数= cursor.count();
cursor.forEach(函数(五)
{
总+ = e.number;
});
平均=总/计数; VAR SESSION_ID = Session.get(ID); VAR测试= Test.findOne({ID:SESSION_ID}); //不起作用
如果(测试){
test.avg =平均;
} 回归测试;
}//路由器
VAR TestRouter = Backbone.Router.extend({
路线:{
:get_id:get_id
},
get_id:功能(get_id){
Session.set(ID,get_id);
的console.log(get_id);
}
});路由器=新TestRouter;Meteor.startup(函数(){
Backbone.history.start({pushState的:真正});
});
的test.html
< HEAD>
<标题>测试与LT; /标题>
< /头><身体GT;
{{>你好}}
< /身体GT;<模板名称=你好>
< H1>的Hello World<!/ H1>
{{#如果测试}}
{{#with测试}}
ID:{{ID}}名称:{{名}} AVG:{{平均}}
{{/带}}
{{/如果}}
< /模板>
model.js
测试=新Meteor.Collection(测试);Test.remove({});如果(Test.find()计数()&酰胺。1)
{
Test.insert({ID:1,
名称:测试1
数量:13}); Test.insert({ID:2,
名称:TEST2
数量:75});
}Meteor.publish(测试,函数(){
返回Test.find();
});
我调试code和发现,在收集ID是一个整数,而SESSION_ID是一个字符串。您需要parseInt函数转换的session_id。
我使用 page.js 路由,这是灵感微客户端路由器防爆preSS路由器,从优秀作品TJ Holowaychuk。
我强烈建议它,因为流星骨干在型号/系列及放一些功能的碰撞;查看/模板。
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();
});
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.
这篇关于MeteorJS:Backbone.js的与路由的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!