我一直在使用评论功能处理应用程序.这导致必须订阅发表评论的集合和评论集合本身.现在它看起来像这样:
<template name="bookView"> {{> book}} {{> comments}} </template>
this.route('book', {
path: '/book/:_id',
template: 'bookView',
waitOn: function() { return Meteor.subscribe('book');},
action: function () {
if (this.ready()){
this.render();
}
else
this.render('loadingTemplate');
},
data: function() {return Books.findOne(this.params._id);}
});
Run Code Online (Sandbox Code Playgroud)
但是现在我想加载属于那本书的所有评论.或者我应该在Template.comments.rendered中处理评论的订阅?
我会在数据铁路由器中使用Meteor.user(),但这在开始时是未定义的...
我正在尝试:
waitOn: function() {
return curretUserHandle;
},
data: function() {
// access to Meteor.user().username, give me undefined
[...]
var curretUserHandle = {
ready: function () {
return 'undefined' !== typeof Meteor.user();
}
};
Run Code Online (Sandbox Code Playgroud)
但是,在curretUserHandle.ready()返回true之前,始终会调用route的数据函数
我知道我可以在数据中添加if(Meteor.user()),但是这个选项不喜欢.
为什么数据不等待Meteor.user()准备好了?
我试图在Router.go中传递查询参数,如下所示:
var filter = 'abc';
var path = Router.current() && Router.current().path;
Router.go(path, {query: {filter: filter}});
Run Code Online (Sandbox Code Playgroud)
但是这并没有改变url,它仍然只加载当前路径而没有查询字符串.但是,如果我手动将查询参数添加到路径,如:
Router.go(path+'?filter='+filter);
Run Code Online (Sandbox Code Playgroud)
这很好用.但是因为我试图用一些过滤的数据加载相同的页面.因此,单击过滤器按钮会反复将过滤器字符串反复附加到路径.
使用铁路由器传递查询字符串的正确方法是什么?
为服务器端路由验证用户的最佳方法(最安全和最简单)是什么?
我正在使用最新的Iron Router 1.*和Meteor 1.*并开始,我只是使用帐户密码.
我有一个简单的服务器端路由,将pdf呈现给屏幕:
两者/ routes.js
Router.route('/pdf-server', function() {
var filePath = process.env.PWD + "/server/.files/users/test.pdf";
console.log(filePath);
var fs = Npm.require('fs');
var data = fs.readFileSync(filePath);
this.response.write(data);
this.response.end();
}, {where: 'server'});
Run Code Online (Sandbox Code Playgroud)
举个例子,我想做一些接近SO答案建议的事情:
在服务器上:
var Secrets = new Meteor.Collection("secrets");
Meteor.methods({
getSecretKey: function () {
if (!this.userId)
// check if the user has privileges
throw Meteor.Error(403);
return Secrets.insert({_id: Random.id(), user: this.userId});
},
});
Run Code Online (Sandbox Code Playgroud)
然后在客户端代码中:
testController.events({
'click button[name=get-pdf]': function () {
Meteor.call("getSecretKey", function (error, response) {
if (error) throw …Run Code Online (Sandbox Code Playgroud) 如何在服务器端路由上检查用户是否已登录?
我会在'之前'添加检查,但是Metor.user()在这里不起作用.
提前致谢.
ps我发现如何让Meteor.user()返回服务器端?,但不适用于铁路由器
我在配置waitOn路由的一部分时遇到问题,其中一个订阅的参数由来自不同订阅的文档中的值确定.
游戏中的收藏品是候选人和访谈.面试将只有一名候选人.这是一些示例数据:
candidate = {
_id: 1
firstName: 'Some',
lastName: 'Developer'
//other props
};
interview = {
_id: 1,
candidateId: 1
//other props
};
Run Code Online (Sandbox Code Playgroud)
路由配置如下.
this.route('conductInterview', {
path: '/interviews/:_id/conduct', //:_id is the interviewId
waitOn: function () {
return [
Meteor.subscribe('allUsers'),
Meteor.subscribe('singleInterview', this.params._id),
// don't know the candidateId to lookup because it's stored
// in the interview doc
Meteor.subscribe('singleCandidate', ???),
Meteor.subscribe('questions'),
Meteor.subscribe('allUsers')
];
},
data: function () {
var interview = Interviews.findOne(this.params._id);
return {
interview: interview,
candidate: Candidates.findOne(interview.candidateId);
}; …Run Code Online (Sandbox Code Playgroud) 我正在努力创建一个用户配置文件页面,使用位于的铁路由器localhost:3000/:username.配置文件页面应具有以下特征:
公共视图和私有视图应存在于同一 URL路径中.根据客户端的凭据,他们会看到一个或另一个没有重定向到其他页面.未找到的页面也不应该重定向,这样如果输入无效的用户名,用户仍然可以在浏览器URL栏中看到无效的URL.
我的router.js文件:
this.route('profile', {
controller: 'ProfileController',
path: '/:username'
});
Run Code Online (Sandbox Code Playgroud)
在内ProfileController,我正在努力拼凑以下内容:
onBeforeAction - 显示加载屏幕; 确定用户名是否存在(即URL是否有效)
waitOn- username在删除加载屏幕之前等待检索数据onAfterAction - 删除加载屏幕谢谢!
我的困境是我想将多个对象属性传递给一个铁:Meteor中的路由器路由.原因是我想传递一个属性来命名我的url和一个属性来查找一个集合项目.它们完全相互独立,我不能使用url属性,因为它不是集合项中的值.这就是我所拥有的:
Template.items.events({
'click': function () {
itemName = this.name.replace(/ /g,'')
Router.go('itemDetails', {itemName: itemName})
}
});
Run Code Online (Sandbox Code Playgroud)
问题是虽然路由器处理这个问题并将我发送到正确的URL,但我不能itemName用来找到我正在寻找的集合项对象(假设这是不可能的).
Router.route('/items/:itemName', {
name: 'itemDetails',
data: function() {return Items.findOne({name: this.params.itemName})}
});
Run Code Online (Sandbox Code Playgroud)
上面的路由器配置不会返回任何内容,因为name != this.params.itemName任何对象.
我试过传递this对象,或者创建具有多个属性的对象,但铁:路由器不会有它.
任何帮助表示赞赏,谢谢.
编辑#1:为了帮助进一步解释问题,我的问题与路由到在URL中使用多个ID的页面相同.例如,我如何将属性传递给iron:路由器来填充:_id和:itemId属性?
Router.route('items/:_id/:_itemId', {
name: 'detailDetails',
data: function() {...}
});
Run Code Online (Sandbox Code Playgroud)
编辑#2:我特别想要做的是将两个属性传递给iron:router并将其中一个属性附加到URL,另一个用于路由的data属性以返回集合项.例:
....
Router.go('itemDetails', {_id: itemBeingPassedId, itemName: nameToBeAppendedToURL})
....
Router.route('/items/:itemName', {
name: 'itemDetails',
data: function(){return Items.findOne(_id)
});
Run Code Online (Sandbox Code Playgroud)
每当我尝试这样做时,它都表示_id未定义.所以基本上,我怎样才能将属性传递给dataURL,而不将其作为URL的一部分并使用this.params?
当我点击我使用IR的Meteor应用程序上不存在的路径时,我得到一个200带有HTML 的响应(当在浏览器上呈现时)在控制台上显示js错误No route found for path: "/aRoute".
如何让它回归404?
我想知道是否有人能够提供一个meteorpad或代码示例,在Meteor中正确使用上面列出的方法之一(使用铁:路由器).我很难理解这些方法究竟是如何与我的应用程序交互的,而且看起来这些方法已经足够新,以至于没有很多关于如何正确使用它们的良好文档.谢谢!