0

所以我想在meteor.js 中创建一个bit.ly 类型的站点。我不知道如何重定向页面。我使用了主干.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>
4

3 回答 3

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');
};
于 2013-05-17T10:23:19.273 回答
0

利用

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

如果要运行更新会话变量的路由器方法,则为 true

于 2013-05-15T20:01:14.693 回答
0

我已经修改了您的 html 模板以帮助解释如何使用 Backbone.history.navigate 进行重定向。我添加了两个名为 content 和 about 的附加模板以及 3 个指向 home、help 和 about 三个路由的静态链接。内容取决于我们的模板助手 homePage、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);
};
}
于 2013-05-15T22:33:12.680 回答