【问题标题】:How to prevent navigating to another page in meteor iron router如何防止在流星铁路由器中导航到另一个页面
【发布时间】:2016-05-04 22:53:47
【问题描述】:

我有一个表单页面,如果用户离开但未保存表单,我想提醒他。 通常我可以通过声明一个函数 window.onBeforeUnload 来达到这个目的。

window.onBeforeUnload = function(){return 'Please save the form before navigating';}

但它似乎在我使用 Meteor 和 Iron Router 的项目中不起作用。 有什么建议吗?

【问题讨论】:

  • “另一个页面”=“您网站上的不同路线”还是“另一个网站上的页面”?
  • 你在哪里添加window.onBeforeUnload = function(){return 'Please save the form before navigating';}
  • @PeppeL-G 另一个页面是我网站中的另一条路线。
  • @MurWade 我在 Template.[template_name].created = function(){...here....} 中添加。当然,我有一个布尔变量来检查何时调用警报函数。
  • @aladine 这个问题也可能有帮助stackoverflow.com/questions/24367914/…

标签: meteor iron-router


【解决方案1】:

这可能是一个 hacky 版本,但它确实有效:

isUnsaved = ->
  return unless @ready()
  unless confirm 'Do you really want to leave the page?'
    Router.go(@url)

Router.onStop isUnsaved,
  only: [
    'editPost'
  ]

【讨论】:

    【解决方案2】:

    旧帖子,但刚刚解决了。这是我的解决方案:

    Router.route('/myPath', {
        unload: function (e, obj) {
            if(Session.get("hasChanged")){
                if (confirm("Are you sure you want to navigate away ?")) {
                    // Go and do some action
                }else{
                    // Redirect to itself : nothing happend
                    this.redirect('/myPath');
               }
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-09-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-11
      相关资源
      最近更新 更多