[英]I'm using iron:router v1.0.7 but this.render() is “undefined”. I can't see what I'm doing wrong
I'm displaying an access denied template when the visitor tries to visit the Submit page. 访问者尝试访问“提交”页面时,我正在显示一个拒绝访问模板。 This works fine.
这很好。 Here is the code:
这是代码:
Router.route('/', {name: 'etoeventsList'});
Router.route('/submit', {name: 'etoeventSubmit'});
var requireLogin = function () {
if (!Meteor.user()) {
this.render('accessDenied');
} else {
this.next();
}
};
Router.onBeforeAction(requireLogin, {only: 'etoeventSubmit'});
I want to utilize "requireLogin" under a different context (anonymous user visiting '/') so I thought I would add an argument to allow me to pass in the template to be rendered. 我想在不同的上下文(匿名用户访问“ /”)下使用“ requireLogin”,所以我想我要添加一个参数以允许我传递要渲染的模板。 Like this:
像这样:
var requireLogin = function (template) { // now with argument 'template'
if (!Meteor.user()) {
this.render(template); // using 'template'
} else {
this.next();
}
};
Router.onBeforeAction(requireLogin('accessDenied'), {only: 'etoeventSubmit'}); // passing 'accessDenied'
Router.onBeforeAction(requireLogin('index'), {only: 'etoeventsList'}); // passing 'index'
The error I receive is Uncaught TypeError: undefined is not a function
and the template I want to show does not display. 我收到的错误是
Uncaught TypeError: undefined is not a function
并且我要显示的模板没有显示。
You can't do it that way. 你不能那样做。 The way you are doing it, this will refer to Window .
你正在做的方式, 这将参考窗口 。 You will have to use an anonymous function as a param to onBeforeAction .
您将必须使用匿名函数作为onBeforeAction的参数。
Router.configure({
layoutTemplate: 'layout',
loadingTemplate: 'loading',
notFoundTemplate: 'notFound',
waitOn: function() {
}
});
if(Meteor.isClient) {
Router.onBeforeAction(function() {
// is user logged in
if (! Meteor.user()) {
if (Meteor.loggingIn()) {
this.render('loading');
} else {
this.render('accessDenied');
}
} else {
this.next();
}
});
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.