Iron-Router更改当前路线之前如何钩住?

Iron-Router更改当前路线之前如何钩住?

问题描述:

我处理的表单中,我的用户将文件上传到S3.如果用户在未验证表单的情况下离开该表单,则即使上传了文件,也应该取消文件上传,并从S3中删除文件.

I work on a form where my user upload files to S3. If the user leave the form without validating it, even if files are uploaded, the files upload should be canceled and the files deleted from S3.

我毫不费力地从S3存储桶中删除了文件.但是,我必须能够在用户离开当前路由(即页面)之前 删除它们,如果下一条路由是同一页面(即页面刷新),则可以跳过它们.

I have no trouble deleting the files from my S3 bucket. However, I need to be able to delete them before the user leaves the current route (i.e. page) and skip it if the next route is the same page (i.e. a page refresh).

这是与 Github上的这个非常相似的问题.他们说那基本上是不可能的. 几个 类似的问题也在其中,但没有一个提供令人满意的答案.

This is an issue very similar to this one on Github where they say that, basically, it can't be done. Several other similar questions are in SO too, but none offers a satisfying answer.

我想检查一下这里是否有可能,因为在我看来这是至关重要的功能,我希望其他人以前也可能已经处理过.

I wanted to check if it is possible here, since it seems to me that this is a vital feature and I expect that other people may have dealt with it before.

如果无法完成,您会建议什么解决方法?

If it can't be done, what workaround would you advise?

我发现了一个很棒的技巧,可以检测用户何时离开当前页面.这是食谱:

I discovered a great trick to detect when the user leave the current page. Here is the recipe:

  • 向路由控制器附加一个订阅(即使为空).这样,route loaded = subscription activeroute left = subscription stopped

添加一个onStop()函数,并将当用户离开页面时需要执行的所有服务器端代码放入其中.这是我的出版物的外观(离开页面后未提交相关表单时,我删除了上传的文件):

Add an onStop() function and put inside it all the server side code you need to execute when the user leave the page. here is how my publication looks (I delete uploaded files when the related form hasn't been submitted when page is left):

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

底线: 据我所知,它就像一种魅力.这是一种跟踪用户活动并在他离开页面或子页面(无论我们将订阅附加到哪个模板路由)后执行代码的好方法.