我在 Iron-Router 中设置了两条路由:“home”(所有帖子的分页列表)和“doc”(详细 View )。主页加载得很好,但只有在之前查看过主页的情况下才能加载详细 View 。否则它将呈现为空——并且不能用作永久链接。
这将始终加载: http://localhost:3000/
仅当之前查看过“home”时才会加载: http://localhost:3000/doc/tZFawq8cgf43hZBaJ
路线:
Router.map(function() {
this.route('home', {
path: '/'
});
this.route('doc', {
path: '/doc/:_id',
data: function() {
return MyPix.findOne({_id: this.params._id});
}
});
});
文档模板:
<template name="doc">
<h1>{{this.name}}</h1>
<img src="{{ this.url store='OriginalRetinaPix' }}" width="{{ this.metadata.width }}" height="{{ this.metadata.height }}" />
</template>
发布/订阅:
Meteor.publish('MyPix', function(cursor) {
Counts.publish(this, 'numberOfPosts', MyPix.find(), { noReady: true });
return MyPix.find({}, {sort: {uploadedAt: -1}, limit: 4, skip: cursor});
});
if(Meteor.isClient) {
Session.setDefault('docCursor', 0);
console.log('docCursor: ' + Session.get('docCursor'));
Meteor.autorun(function(){
Meteor.subscribe('MyPix', Session.get('docCursor'));
})
}
顺便说一句:GitHub 上的项目
最佳答案
在“doc”路线上,您应该使用 waitOn
以便在页面加载时准备好数据。在 Router.configure
中添加加载模板
我建议您升级到新的 iron:router
路由声明,并添加 meteorhacks:subs-manager
以更好地缓存订阅。
这是一个应该适用于您的情况的示例
var subs = new SubsManager();
Router.route('/doc/:_id', {
name: 'doc',
template: 'doc',
waitOn: function() {
return subs.subscribe('aPix', this.params._id);
},
data: function() {
return {
apix: MyPix.findOne({
_id: this.params._id
})
};
}
});
并在服务器端创建一个publications.js
Meteor.publish('aPix', function(id) {
check(id, String);
return MyPix.find(id);
});
关于javascript - 单 View 页面永久链接为空,带有 Iron Router,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28168471/