简体   繁体   中英

alert user when leaving route in Ember 2.0

Ok so i have a component i made up with a form. I would like to alert the user that he has unsaved data (if he has) when the user clicks on another link. Now if i would not be tied to ember i would put a flag on a change event and add a window unload listener to show the actual message, However, Since im not really leaving the window this is not getting called.

Is there any Ember event i can attach a handler to? like a change route intent event of some kind

Im interesting in knowing which is the correct event for the leaving route action, not in the something change logic.

ive tried this so far on the didInsertElement( ) method

Ember.$("body").on('unload',()=> alert('it works'));     
Ember.$("window").on('unload',()=> alert('it works'));   
Ember.$("body").on('unload','body'()=> alert('it works'));  

any ideas?

Have you had a look at the willTransition method on your route? That will be the place to do it. Have a read of this guide to preventing and retrying transitions , as well as the willTransition method's documentation . Hope that helps!

You can catch transition event on route. It's meant to be used for this use case. There is also action when component is being destroyed but that is meant for teardown.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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