0

ということで、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>
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 に答える