Based on http://stackoverflow.com/a/11844403/120067 but with the following enhancements:
- It is coffeescript which IMHO automatically makes it better. :)
- It is entirely based on event bubbling so dynamic content is automatically handled (@AlphaMale's update also has this).
- It only operates on POST forms as GET forms do not have data we typically want to avoid loosing (i.e. GET forms tend to be search boxes and filtering criteria).
- It doesn't need to be bound to a specific button for carrying out the save. Anytime the form is submitted we assume that submission is saving.
- It is Turbolinks compatible. If you don't need that just drop the two page: event bindings.
- It is designed so that you can just include it with the rest of your JS and your entire site will be protected.
Install with gist-dep with:
gist-dep add -p app/assets/javascripts/unsaved-alert.js.coffee 77720eb0da524a049c2b