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

Apply "dirty" class to each dirty field? #74

Open jacobalberty opened 9 years ago

jacobalberty commented 9 years ago

Would it be to have AreYouSure apply the "dirty" class to the dirty fields themselves? or is there some other functionality to check if a field is dirty? This would allow submit handlers to do things like disable clean fields and submit only the dirty ones.

elpd commented 9 years ago

I am not a developer for the project. If I understand you correctly, you want to know every input that is dirty and send only those to the server.

I think you can achieve this with a custom function that you can call when you are about to do the submit request.

That function needs to loop through all the form inputs, and collect only the dirty ones. You can know which field is dirty by comparing the data-ays-orig attribute on the input field if it has one - which hold the original value for the field, to the current field value of the input field. The set of field inputs to be consider for looping can be identified like AYS do with fieldSelector.

codedance commented 9 years ago

I'd like to better understand the use-cases. It would be pretty easy to add a new class to the dirty field. It will add a bit of extra code complexity and some overhead, so I'd like to verify that it will be useful enough. At the moment the code "short circuits" on the first dirty field and hence avoids the need to check all fields on each keypress. To mark all dirty fields the whole form would need to be walked.

Use Cases:

Please let me know if you can think of any others.

Thinking out loud:

I'd welcome input on the thinking/solutions above.

Interim solution: As suggested by @elpd , the isFieldDirty function implements that logic that you're after. You could implement this in your own code.

jacobalberty commented 9 years ago

The interim solution of course works, I have some concerns over maintaining the code to handle checking if its dirty inside my own code base (its farfetched but what if the internals of ays change rendering my own code inoperable?)

Not knowing anything about extending jquery how feasable is it to expose isFieldDirty as an alternative? It wouldn't make use case 2 any easier but it would allow 1 and 3 use cases while reusing existing code and avoiding the peformance hit.

codedance commented 9 years ago

I think the request/ideas are valid and should be considered for inclusion. Here are the options as I see it:

Option 1) Add a non-default option to mark dirty field with a class. e.g.

// Add a class 'ays-dirty' to all dirty fields.
// Sight performance hit - not appropriate for large forms.
$('form').areYouSure( {'markDirtyFields':true} );

Option 2) Add a utility method to quickly query dirty fields.

// Return a collection of dirty fields when called.
$dirtyFields = $('form').areYouSureDirtyFields()
//... do work with fields (e.g. color, count, check)

Option 3) Both 1 and 2 But this would mean more work! I'd prefer to focus on one first and then reassess as there is demand.

My current thinking is Option 1 is most flexible.

I'd welcome some votes!

jacobalberty commented 9 years ago

Option 1 would be the easiest to migrate from my interim soltuion to so it has my vote.

jacksontong commented 8 years ago

I vote for option 1

infosaurus commented 8 years ago

Hi, I too could use that feature :)

Options 1 and 2 both seem OK.

By the way, I couldn't get the workaround solution to work -- inside isFieldDirty(), the variable origValue is always undefined for all fields.

var origValue = $field.data('ays-orig');

Could it be that the 'ays-orig' class is removed from all fields after .areYouSure() has finished executing ?

josephpconley commented 8 years ago

+1 for option 1

guyoron commented 7 years ago

+1 for this and for option 1

AlejandroFrias commented 6 years ago

+1 for option 2. I only need the list of dirty fields at time of submit and don't want to be forced to incur the overhead costs during form editing.