流星铁路由器动态段不工作

时间:2013-12-06 21:11:50

标签: javascript meteor segment iron-router

我一直在寻找流星,它太棒了!我一直在寻找路由解决方案,而且我找到了“铁路由器”#39;这很酷,我设法得到静态页面使用模板,但当我去/ 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');
    }
  });
}

blog.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>

3 个答案:

答案 0 :(得分:1)

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

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

答案 1 :(得分:0)

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

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

答案 2 :(得分:0)

也许我错过了什么,但是在你的控制器中你说

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

但是在您的模板中,您使用的是{{body}},我看不到任何地方都分配了任何内容。不需要像:

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