流星:使用Iron Router修改路线更改时的集合 [英] Meteor: Modify collection on a route change using Iron Router

查看:55
本文介绍了流星:使用Iron Router修改路线更改时的集合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我正在开发一个论坛类型的应用程序.当用户导航到帖子时,URL将为www.myapp.com/post/POSTID.我正在使用Iron Router来处理路由.

So I'm working on a forum type app. When the user navigates to a post, the url will be www.myapp.com/post/POSTID. I'm using Iron Router to handle the routing.

进入和离开该帖子后,我想update一个集合来指示用户已看到该帖子.我知道在Iron Router中,如果您修改路由所订阅的基础订阅之一,则会遇到无限的反应性循环.我以前曾尝试在action()中更新集合,但遇到了无限循环.

Upon entering AND leaving this post, I want to update a collection to indicate that the user has seen the post. I know that in Iron Router, if you modify one of the underlying subscriptions that the route is subscribed to, you run into an infinite reactivity loop. I was trying to update the collection in action() before, but got an infinite loop.

因此,我在路径中将update移到了onRun().无限循环消失了,但是onRun()的问题在于它不会在热重载时触发.每次更改代码并重新装入流星时,均不会调用onRun()代码.我必须手动重新加载浏览器的ctrl-R才能真正调用onRun()代码.

So I moved the update to onRun() in the route. The infinite loop is gone, but the problem with onRun() is that it won't trigger on hot reload. Every time I make a code change and Meteor hot reloads, the onRun() code is not called. I have to manually do a ctrl-R reload of my browser to get the onRun() code to be actually called.

推荐答案

出于您的目标,出于多种原因,我不会使用iron-router函数来更新此类值:

I wouldn't use the iron-router function to update this kind of value for several reason, starting with your objective:

我想更新一个收藏集以表明用户已经看过帖子

I want to update a collection to indicate that the user has seen the post

如果需要的话,至少可以做的就是在渲染模板后立即进行更新.如果您不这样做,则只需单击指向您的页面的链接,而该链接被另一个页面中止,则将其标记为用户可见.

If you want that, the least you can do is update once the template has been rendered. If you don't, a simple click on a link to your page aborted by another one will flag it as seen by the user.

更好的是,您可以观看active事件并在该事件上触发setInterval().这样,您可以确保

Even better, you can watch the active event and trigger a setInterval() on this event. This way you can make sure that

  1. 页面已加载
  2. 页面处于活动状态
  3. 它已激活至少x秒

它看起来像这样:

Template.yourTemplate.rendered = function() {
$(window).focus(function() { //for 3 seconds
    Meteor.setInterval(function(){
      //your update query
    }, 3000);
});

拦截用户离开页面的部分比较棘手.我只是找到了一种方法,所以我会分享.

The part where you intercept the user leaving the page is trickier. I just found a way to do it so I will share.

  • 订阅您的路由控制器(您已经这样做了).这样,route loaded = subscription activeroute left = subscription stopped

在发布函数中添加一个onStop()函数,并将当用户离开页面时需要执行的所有服务器端代码放入其中.就您而言,这将是与显示的帖子相关的更新.这是我的出版物的外观(离开页面后未提交相关表单时,我会删除上传的文件),您应该能够对其进行调整以适应您的需求:

Add an onStop() function to the publication function and put inside it all the server side code you need to execute when the user leave the page. In your case, it would be an update related to the displayed post. Here is how my publication looks (I delete uploaded files when the related form hasn't been submitted when page is left), you should be able to adapt it to fit into your needs:

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();
});
};

这篇关于流星:使用Iron Router修改路线更改时的集合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆