查看“故事”时,我希望自动订阅该故事并在更改页面时更改订阅的故事。流星路线和自动订阅
这就是我得到的:它似乎工作,但多个autosubscribe似乎错了?
route("stories/:storytitle/:storyID", function(storyTitle, storyID) {
Session.set('storyID', storyID)
Meteor.autosubscribe(function() {
var storyID = Session.get('storyID');
if (storyID)
Meteor.subscribe("story", Session.get("storyID"), function() {
Router.goto('story')
});
});
});
Template.story.data = function() {
var storyID = Session.get('storyID');
var story = Stories.findOne({
_id: storyID
})
return story;
};
这似乎更符合我在寻找的一般,但有大量的样板。将查询放入路由中似乎也是错误的,而不是仅仅将它放在模板助手中。
route("stories/:storytitle/:storyID", function(storyTitle, storyID) {
Session.set('storyID', storyID)
var story = Stories.findOne({
_id: storyID
})
if (story)
Router.goto('story')
});
Meteor.autosubscribe(function() {
var storyID = Session.get('storyID');
if (storyID)
Meteor.subscribe("story", Session.get("storyID"), function() {
Router.goto('story')
});
});
Template.story.data = function() {
var storyID = Session.get('storyID');
var story = Stories.findOne({
_id: storyID
})
return story;
};
这些都是正确的方法吗?如何在故事中保留自动订阅,并在更改页面时自动更改订阅?
凭直觉我就试试这个:
route("stories/:storytitle/:storyID", function(storyTitle, storyID) {
Session.set('storyID', storyID)
Router.goto('story')
});
Meteor.autosubscribe(function() {
var storyID = Session.get('storyID');
if (storyID)
Meteor.subscribe("story", Session.get("storyID"), function() {
Router.goto('story')
});
});
这根本不起作用。它会尝试在故事加载之前转到故事路线并引发白屏或错误。
我正在使用你的反应式路由器,这很好,谢谢它:) – Harry
谢谢!我刚刚添加了第四点,我忘了我想说。 –