2013-05-15 42 views
0

所以我想在meteor.js中創建一個bit.ly類型的站點。我無法弄清楚如何重定向關閉頁面。我使用backbone.js的路線,這是工作。理想情況下,它會從數據庫中獲取鏈接,創建鏈接並重定向到它。我試過了window.location但是,這並不正常工作 js文件:meteorjs中的重定向路由

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> 

回答

1

導航到外部URL。你可以覆蓋路由器導航

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'); 
}; 
+0

如果你不想重載導航,那麼只需使用這個'window.location.href ='http://google.com';' – jamin79

0

使用

Backbone.history.navigate('/help', trueorfalse); 

真,如果你想運行在路由器方法更新會話變量

+0

你可以擴展一下,我對流星/主幹還是比較陌生的。也許是一個重定向的例子? –

0

我修改了您的html模板以幫助解釋使用Backbone.history.navigate重定向。我添加了兩個額外的模板,稱爲內容和約三個靜態鏈接指向三個路線回家,幫助和約。內容取決於什麼我們的模板助手主頁,helpPage或aboutPage返回現在

<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); 
}; 
} 
+0

這是有效的,但當我嘗試將它引導到異地時不起作用。 'Backbone.history.navigate(「http://google.com」,true);'轉到:'localhost:3000/http:// google.com'。你將如何導航到不同的域名? –

+0

我要添加另一個答案 – jamin79