所以我想在meteor.js中创建一个bit.ly类型的站点。我无法弄清楚如何重定向页面。我使用backbone.js作为路由,这些路由正在运行。理想情况下,它会从数据库中获取链接,创建链接并重定向到它。我试过window.location但是这不能正常工作 js文件:
if (Meteor.isClient) {
var Router = Backbone.Router.extend({
routes: {
"" : "main",
"/" : "main",
"help" : "help",
'help/' : "help",
},
main: function() {
Session.set('currentPage', 'homePage');
},
help: function() {
Session.set('currentPage', 'helpPage');
}
});
var app = new Router;
Meteor.startup(function () {
Backbone.history.start({pushState: true});
});
Template.home.homePage = function(){
return Session.get("currentPage") == 'homePage';
};
Template.help.helpPage = function(){
return Session.get("currentPage") == 'helpPage';
//I want to do a redirect here somewhere:
//window.location = 'http://google.com';
};
}
HTML:
<head>
<title>My app name</title>
</head>
<body>
{{> home}}
{{> help}}
</body>
<template name="home">
{{#if homePage}}
<h1>Home Page</h1>
{{/if}}
</template>
<template name="help">
{{#if helpPage}}
<h1>Help Page</h1>
{{/if}}
</template>
答案 0 :(得分:1)
导航到外部网址。您可以覆盖路由器导航
if (Meteor.isClient) {
var Router = Backbone.Router.extend({
routes: {
"" : "main",
"help" : "help",
'about' : "about",
},
navigate: function (url) { window.location = url; },
main: function() {
Session.set('currentPage', 'homePage');
},
help: function() {
Session.set('currentPage', 'helpPage');
},
about: function() {
Session.set('currentPage', 'about');
}
});
var app;
Meteor.startup(function () {
app = new Router;
Backbone.history.start({pushState: true});
});
Template.content.homePage = function(){
return Session.get("currentPage") == 'homePage';
};
Template.content.helpPage = function(){
return Session.get("currentPage") == 'helpPage';
};
Template.content.aboutPage = function(){
return Session.get("currentPage") == 'about';
};
//I will add a helper below that will redirect to google everytime about link is clicked. Open console to see message logged before redirect
Template.about.rendered = function(){
console.log('About is about to be rendered but wait..... we redirect to google.com');
app.navigate('http://google.com');
};
答案 1 :(得分:0)
使用
Backbone.history.navigate('/help', trueorfalse);
如果要运行更新会话变量的路由器方法
,则为true答案 2 :(得分:0)
我修改了你的html模板,以帮助解释如何使用Backbone.history.navigate重定向。我添加了两个额外的模板,称为内容和约3个静态链接,指向三个路由回家,帮助和约。内容取决于我们的模板助手homePage,helpPage或aboutPage return
<head>
<title>My app name</title>
</head>
<body>
<a href='/'>Home</a>
<a href='/help'>Help</a>
<a href='/about'>About</a>
{{> content}}
</body>
<template name='content'>
{{#if homePage}}
{{> home}}
{{/if}}
{{#if helpPage}}
{{> help}}
{{/if}}
{{#if aboutPage}}
{{> about}}
{{/if}}
</template>
<template name="home">
<h1>Home Page</h1>
</template>
<template name="help">
<h1>Help Page</h1>
</template>
<template name="about">
<h1>About Page</h1>
</template>
JS现在
if (Meteor.isClient) {
var Router = Backbone.Router.extend({
routes: {
"" : "main",
"help" : "help",
'about' : "about",
},
main: function() {
Session.set('currentPage', 'homePage');
},
help: function() {
Session.set('currentPage', 'helpPage');
},
about: function() {
Session.set('currentPage', 'about');
}
});
Meteor.startup(function () {
var app = new Router;
Backbone.history.start({pushState: true});
});
Template.content.homePage = function(){
return Session.get("currentPage") == 'homePage';
};
Template.content.helpPage = function(){
return Session.get("currentPage") == 'helpPage';
};
Template.content.aboutPage = function(){
return Session.get("currentPage") == 'about';
};
//I will add a helper below that will redirect to homePage everytime about link is clicked. Open console to see message logged before redirect
Template.about.rendered = function(){
console.log('About is about to be rendered but wait..... we redirect to home');
Backbone.history.navigate( "/", true);
};
}