我一直在研究流星,它太棒了!我一直在寻找路由解决方案,我发现'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>