简体   繁体   English

OJET - 在离开我的页面之前如何提醒用户?

[英]OJET - How do I alert a user before leaving my page?

I am working on a single page application using Oracle JET.我正在使用 Oracle JET 开发单页应用程序。 In the page I'm building, I'm trying to alert the user when they try to leave the page without saving their changes.在我正在构建的页面中,我试图在用户尝试离开页面而不保存更改时提醒他们。 So when leaving the page I want to:因此,当离开页面时,我想:

  • Intercept the routing.拦截路由。
  • Check if there are any changes.检查是否有任何变化。
  • Give a popup if there are changes.如果有更改,请弹出一个窗口。
  • Continue with routing if there aren't any unsaved changes.如果没有任何未保存的更改,请继续路由。

I believe that this should be possible using the ojRouter ( doc link here ), but I have not gotten it to work.我相信这应该可以使用 ojRouter ( 此处的文档链接),但我还没有让它工作。 I have tried many approaches, but this is what I have ended up with so far:我已经尝试了很多方法,但这是我到目前为止的结果:

EDIT: I have gotten a little further and managed to successfully access the canExit property, I edited my code block below.编辑:我已经更进一步并成功地访问了 canExit 属性,我在下面编辑了我的代码块。 However, this still does not block routing.但是,这仍然不会阻止路由。

oj.Router.sync().then(() => {
   const letrouterState = oj.Router.rootInstance;
   letrouterState.currentState().canExit = ableToExit();
});

function ableToExit() {
  let canExit;
  //function that will check if the page has unsaved changes
  if (ableToExit()) {
    canExit = false;
  } else {
    canExit = true;
  }
  return canExit;
}

I have also tried to use window.onhashchange .我也尝试过使用window.onhashchange However, this only triggers an event when opening the page my page, not when leaving it.但是,这只会在打开我的页面时触发事件,而不是在离开时触发。

I hope I made my problem clear and I am happy to clarify any of my request if needed.我希望我把我的问题说清楚了,如果需要的话,我很乐意澄清我的任何要求。 Would someone be able to help me?有人可以帮助我吗? Thank you for your time in advance.提前感谢您的时间。

-Wouter -沃特

The ModuleViewModel interface implements the optional canExit method, which gets invoked on the beforeStateChange event of ModuleRouterAdapter . ModuleViewModel接口实现了可选的canExit方法,该方法在ModuleRouterAdapterbeforeStateChange事件上被调用。 Assuming you use ModuleRouterAdapter in conjunction with <oj-module> , this is relatively easy to implement, you simply need to return a Promise and then either resolve or reject that.假设您将ModuleRouterAdapter<oj-module>结合使用,这相对容易实现,您只需返回一个Promise然后解决或拒绝它。

Because intervening with the entire router became a very messy solution, we have decided to adjust the requirements and not do this anymore.因为干预整个路由器变成了一个非常混乱的解决方案,我们决定调整需求,不再这样做。 Thanks to anyone that tried to help.感谢任何试图提供帮助的人。 Moral of the story is not to try this:).故事的寓意是不要尝试这个:)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM