jquery.AreYouSure icon indicating copy to clipboard operation
jquery.AreYouSure copied to clipboard

Apply "dirty" class to each dirty field?

Open jacobalberty opened this issue 10 years ago • 10 comments

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.

jacobalberty avatar Feb 23 '15 03:02 jacobalberty

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.

elpd avatar Feb 23 '15 21:02 elpd

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:

  • @jacobalberty 's request to disable clean fields and or submit dirty ones.
  • Change styling on dirty fields to draw users attention to the fields they have changed.
  • Short-cut validation logic. Only validate dirty fields.

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

Thinking out loud:

  • Could be a non-default option leading to minimal performance hit out of the box.
  • Rather than mark with a dirty class, an alternate solution would be to provide a utility method (selector style) to return dirty fields. That way the performance hit would only occur when called. e.g. $myform.areYouSureDirtyFields().each(...)

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.

codedance avatar Feb 23 '15 22:02 codedance

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.

jacobalberty avatar Feb 24 '15 00:02 jacobalberty

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!

codedance avatar Feb 26 '15 03:02 codedance

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

jacobalberty avatar Feb 27 '15 22:02 jacobalberty

I vote for option 1

jacksontong avatar Sep 15 '15 04:09 jacksontong

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 ?

infosaurus avatar Oct 09 '15 13:10 infosaurus

+1 for option 1

josephpconley avatar Jan 05 '16 16:01 josephpconley

+1 for this and for option 1

guyoron avatar May 17 '17 18:05 guyoron

+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.

AlejandroFrias avatar Aug 24 '17 16:08 AlejandroFrias