如何防止在流星铁路由器中导航到另一个页面

时间:2015-01-08 10:36:06

标签: meteor iron-router

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

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

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

2 个答案:

答案 0 :(得分:1)

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

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

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

答案 1 :(得分:1)

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

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');
           }
        }
    }
}
相关问题