codedance / jquery.AreYouSure

A light-weight jQuery "dirty forms" Plugin - it monitors html forms and alerts users to unsaved changes if they attempt to close the browser or navigate away from the page. (Are you sure?)
508 stars 145 forks source link

Detecting activity in form inputs. Intended for delyed ajax save #75

Open elpd opened 9 years ago

elpd commented 9 years ago

Thank you, great library.

For a project I am working on, I needed to delay the Ajax save operation until 2 seconds after the user stopped typing / stopped editing the form inputs.

I have changed the AreYouSure code to trigger a custom activityDetected.areYouSure event and I bind the trigger function to the form inputs in in the same places where the checkForm function is bind to. https://github.com/elpd/jquery.AreYouSure/commit/eea0460d81a068e775c3fe5b06838573facfa0bd

I then use that custom event and bind the logic for the Ajax timer to that event in my project. I use the 'clean.areYouSure' event to stop the timer if before it expired, the user changed the data to its former value.

Does this look like a good way to achieve my goal ? Is this custom event concept looks like it indeed should be in the library code ?

codedance commented 9 years ago

Hey @elpd,

Thanks for submitted your ideas. Your approach is valid and I can see why and how you've implemented this. My initial gut-feel is that this does not belong in the areYouSure library because it's more of a general form change detection rather than dirty/unsaved state change detection. As mentioned on the project page, the focus is to keep it as light weight as possible (do one thing and do it well) so I've been pretty ruthless on both myself and others when it comes to adding new features. Even the iOS support has been done outside as a shim/plugin rather than core in the code.

My thinking is that it's best done as a new plugin - or even inline in your own code. The good thing about areYouSure is that's been designed in a way where will sit nicely alongside other plugins or events on the same DOM elements (everything is nicely namespace).

Of course, running a form of the code for your own project is very valid so please feel free to keep doing this. I'll be pushing a new version soon but merging in changes on top of your additions would be relatively easy.

Thanks for your input and I'm glad you're finding areYouSure useful.