2

我一直在研究流星,它太棒了!我一直在寻找路由解决方案,我发现'iron-router' 很酷,我设法让获取静态页面与模板一起使用,但是当我转到 /posts/123 时,页面没有呈现。我一直在关注这个网站上的视频

https://www.eventedmind.com/feed/q8QWX5e7PTu8BEReY

这是我的代码

blog.js 已编辑

Posts = new Meteor.Collection('posts');

Router.configure({
  layout: 'layout',
  loadingTemplate: 'loading',
  notFoundtemplate: 'notFound'
});

Router.map( function (){
  this.route('posts', {
    path: '/',
    waitOn: function () {
      return App.subs.posts;
    },
    data: {
      posts: function () {
        return Posts.find({}, {sort: {order: 1}});
      }
    }
  });

  this.route('postShow', {
    path: '/posts/:_id'
  });
});

if (Meteor.isServer) {
  Meteor.publish('posts', function () {
    return Posts.find({}, {sort: {order: 1}});
  });

  Meteor.publish('post', function (id) {
    return Posts.find({_id: id});
  });
}

if (Meteor.isClient) {
  App = {
    subs: {
      posts: Meteor.subscribe('posts')
    }
  };

  PostShowController = RouteController.extend({
    template: 'postShow',
    before: function () {
      var _id = this.params._id;

      if(App.subs.post)
        App.subs.post.stop();

      App.subs.post = Meteor.subscribe('post', _id);
    },
    data: {
      body: function () {
        return Posts.findOne({_id: this.params._id});
      }
    },
    run: function () {
      this.render('postShow');
    }
  });
}

博客.html

<head>
    <title>IronRouter</title>
</head>

<body>
</body>

<template name="layout">
    <div class="container">
        <aside class="sidebar">
            <div class="sidebar-inner">
                {{yield 'sidebar'}}
            </div>
        </aside>

        <section class="content">
            <div class="content-inner">
                {{yield}}
            </div>
        </section>
    </div>
</template>

<template name="notFound">
  <h1>Not Found</h1>
</template>

<template name="loading">
  <h1>Loading...</h1>
</template>

<template name="posts">
  <h1>Posts</h1>
  <ul>
    {{#each posts}}
    <li>{{> postListItem}}</li>
    {{/each}}
  </ul>
</template>

<template name="postListItem">
    <a href="{{pathFor 'postShow'}}">
        {{title}}
    </a>
</template>

<template name="postShow">
  <p>
    {{body}}
  </p>
</template>

<template name="postShowSidebar">
  <h1>{{title}}</h1>
</template>
4

3 回答 3

1

我是视频的作者,可能有点过时了。我很抱歉。我会做以下事情:

  1. 更改layout: 'layout'layoutTemplate: 'layout'
  2. run从控制器中删除该方法。该方法实际上做了很多,因此如果您想覆盖渲染,您可以使用该action方法。但在你的情况下,你不需要做任何事情,因为它会自动发生。
  3. 这有点不相关,但您不需要自己停止订阅。事实证明,Meteor 和 Iron-router 会在计算停止时自动执行此操作(即当您导航到新路线时)。

我希望这有帮助!

于 2013-12-13T04:19:44.260 回答
0

尝试将您的 PostShowController 添加到相应的路由:

this.route('postShow', {
  path: '/posts/:_id',
  controller: 'PostShowController'
});
于 2013-12-06T22:14:49.997 回答
0

也许我错过了一些东西,但在你的控制器中你说

data: function () {
  return Posts.findOne({_id: this.params._id});
},

但是在您的模板中,您使用的是 {{body}},我看不到任何地方都为其分配了任何东西。是不是必须这样:

data: {
  body: function () {
    return Posts.findOne({_id: this.params._id});
  }
}
于 2013-12-06T23:12:05.107 回答