流星:使用 Iron Router 修改路线更改的集合
Meteor: Modify collection on a route change using Iron Router
所以我正在开发一个论坛类型的应用程序。当用户导航到 post 时,url 将是 www.myapp.com/post/POSTID
。我正在使用 Iron Router 来处理路由。
在进入和离开这个 post 后,我想 update
一个集合来表明用户已经看到 post。我知道在 Iron Router 中,如果您修改订阅该路由的其中一个基础订阅,您 运行 会进入无限反应循环。我之前尝试更新 action()
中的集合,但遇到了无限循环。
所以我把路线中的update
移到了onRun()
。无限循环消失了,但是 onRun()
的问题是它不会在热重载时触发。每次我更改代码和 Meteor 热重载时,都不会调用 onRun()
代码。我必须手动 ctrl-R
重新加载我的浏览器以获取实际调用的 onRun()
代码。
出于多种原因,我不会使用 iron-router 功能来更新此类值,首先是您的 objective:
I want to update a collection to indicate that the user has seen the post
如果需要,至少可以在模板呈现后进行更新。如果您不这样做,只需单击一个 link 到您的页面,该页面被另一个页面中止,就会将其标记为用户所见。
更好的是,您可以观看 active
事件并在该事件上触发 setInterval()
。这样你就可以确保
- 页面已加载
- 页面处于活动状态
- 它至少已激活 x 秒
看起来像这样:
Template.yourTemplate.rendered = function() {
$(window).focus(function() { //for 3 seconds
Meteor.setInterval(function(){
//your update query
}, 3000);
});
拦截用户离开页面的部分比较棘手。我刚刚找到了一种方法,所以我会分享。
订阅您的路由控制器(您已经这样做了)。这样,route loaded
= subscription active
和 route left
= subscription stopped
在发布函数中添加一个onStop()
函数,并将用户离开页面时需要执行的所有服务器端代码放入其中。在您的情况下,这将是与显示的 post 相关的更新。这是我的出版物的样子(我在页面离开时没有提交相关表格时删除上传的文件),你应该能够适应它以适应你的需要:
Meteor.publish("files", function(sessionId) {
var self = this;
// Here I clean all the files I need to remove because the user has
// not submitted the current form.
self.onStop(function () {
console.log (sessionId + " is cleaning...");
cleanFiles(sessionId)
});
// I look for files related to the current upload session only
if(Users.isInRoles(this.userId, ["user"])) {
return Files.find({"session_id":sessionId, "owner":this.userId}, {});
}
//and I make my publication available in case the if statement failed
return self.ready();
});
};
所以我正在开发一个论坛类型的应用程序。当用户导航到 post 时,url 将是 www.myapp.com/post/POSTID
。我正在使用 Iron Router 来处理路由。
在进入和离开这个 post 后,我想 update
一个集合来表明用户已经看到 post。我知道在 Iron Router 中,如果您修改订阅该路由的其中一个基础订阅,您 运行 会进入无限反应循环。我之前尝试更新 action()
中的集合,但遇到了无限循环。
所以我把路线中的update
移到了onRun()
。无限循环消失了,但是 onRun()
的问题是它不会在热重载时触发。每次我更改代码和 Meteor 热重载时,都不会调用 onRun()
代码。我必须手动 ctrl-R
重新加载我的浏览器以获取实际调用的 onRun()
代码。
出于多种原因,我不会使用 iron-router 功能来更新此类值,首先是您的 objective:
I want to update a collection to indicate that the user has seen the post
如果需要,至少可以在模板呈现后进行更新。如果您不这样做,只需单击一个 link 到您的页面,该页面被另一个页面中止,就会将其标记为用户所见。
更好的是,您可以观看 active
事件并在该事件上触发 setInterval()
。这样你就可以确保
- 页面已加载
- 页面处于活动状态
- 它至少已激活 x 秒
看起来像这样:
Template.yourTemplate.rendered = function() {
$(window).focus(function() { //for 3 seconds
Meteor.setInterval(function(){
//your update query
}, 3000);
});
拦截用户离开页面的部分比较棘手。我刚刚找到了一种方法,所以我会分享。
订阅您的路由控制器(您已经这样做了)。这样,
route loaded
=subscription active
和route left
=subscription stopped
在发布函数中添加一个
onStop()
函数,并将用户离开页面时需要执行的所有服务器端代码放入其中。在您的情况下,这将是与显示的 post 相关的更新。这是我的出版物的样子(我在页面离开时没有提交相关表格时删除上传的文件),你应该能够适应它以适应你的需要:Meteor.publish("files", function(sessionId) { var self = this; // Here I clean all the files I need to remove because the user has // not submitted the current form. self.onStop(function () { console.log (sessionId + " is cleaning..."); cleanFiles(sessionId) }); // I look for files related to the current upload session only if(Users.isInRoles(this.userId, ["user"])) { return Files.find({"session_id":sessionId, "owner":this.userId}, {}); } //and I make my publication available in case the if statement failed return self.ready(); }); };