【发布时间】:2014-04-28 13:53:49
【问题描述】:
我有一个小流星应用程序与铁路由器一起使用。这是我的 routes.js,客户端和服务器都可以使用:
Router.configure({
layoutTemplate: 'defaultLayout',
loadingTemplate: 'loading'
});
// Map individual routes
Router.map(function() {
this.route('comicCreate', {
path: '/comic/create'
});
this.route('comicDetails', {
onBeforeAction: function () {
window.scrollTo(0, 0);
},
path: '/comic/:_id',
layoutTemplate: 'youtubeLayout',
data: function() { return Comics.findOne(this.params._id); }
});
this.route('frontPage', {
path: '/',
layoutTemplate: 'frontPageLayout'
});
this.route('notFound', {
path: '*',
where: 'server',
action: function() {
this.response.statusCode = 404;
this.response.end('Not Found!');
}
});
});
我需要将我的漫画收藏文档中的一些字段输入到包装 Youtube 的 IFrame API 的包中,我正在通过渲染函数执行此操作:
Template.comicDetails.rendered = function() {
var yt = new YTBackground();
if (this.data)
{
yt.startPlayer(document.getElementById('wrap'), {
videoIds: this.data.youtubeIds,
muteButtonClass: 'volume-mute',
volumeDownButtonClass: 'volume-down',
volumeUpButtonClass: 'volume-up'
});
}
};
当我首先转到 localhost:3000/ 然后单击设置为“{{pathFor 'comicDetails'_id}}”的链接时,这非常有用。但是,如果我直接访问 localhost:3000/comic/somevalidid,则不会,尽管两条路线最终都指向同一个 url。
原因似乎是当我直接进入深层链接时,“this.data”在渲染函数执行期间未定义。模板本身显示得很好(数据正确替换了我模板中的空格键 {{field}} 标记),但是当渲染的回调触发时,“this”上下文中没有任何数据。
我在这里做错了吗?
【问题讨论】:
标签: meteor iron-router