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

ala*_*ine 5 meteor iron-router

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

window.onBeforeUnload = function(){return 'Please save the form before navigating';}
Run Code Online (Sandbox Code Playgroud)

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

pmu*_*ens 1

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

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

Router.onStop isUnsaved,
  only: [
    'editPost'
  ]
Run Code Online (Sandbox Code Playgroud)