【问题标题】:How to redirect a user when the published cursor gets emptied?当发布的光标被清空时如何重定向用户?
【发布时间】:2015-12-21 08:08:53
【问题描述】:

我正在发布一个游标,它只在其中输出一个文档:

Meteor.publish ( 'test', function () {
  return MyCollection.find ( 'DocumentMongoDBID' );
}

我正在从路由器订阅它(使用铁路由器):

Router.route ( '/thepage', {
  ...
  subscriptions: function () {
    this.subscribe ( 'test' ).wait ();
  }
}

如果文档被删除,我想将用户重定向到另一个页面,因此如果光标为空。

发生这种情况时如何调用函数以便重定向用户?依赖该订阅的所有模板助手都会被调用,但我更希望在它们之前调用另一个函数,最终将重定向用户。

【问题讨论】:

  • 你订阅的怎么样了?全局,路由器,模板等......
  • @DavidWeldon 我已经更新了包括订阅代码在内的问题。

标签: redirect meteor publish iron-router subscribe


【解决方案1】:

也许尝试在客户端使用LocalCursor.observeChanges

Meteor.startup(() => {
  MyCollection.find('DocumentMongoDBID').observeChanges({
    removed(){
      // redirect the user
      Router.go(...);
    }
  });
});

https://docs.meteor.com/#/full/observe_changes

【讨论】:

  • 此代码应该在客户端上运行,独立于您的发布代码。
猜你喜欢
  • 1970-01-01
  • 2021-11-17
  • 2019-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-11-01
  • 1970-01-01
相关资源
最近更新 更多