State of Binder#hasChanges() after user takes back changes

Viewed 65

For an binder without a bound bean, the hasChange() method returns true as soon as a bound field is changed. But it's still true after the change gets reverted. So I'm looking for something like isEqualToInitialState(), in order to enable the save button for real changes only.

Is there a best practice for my intention? I also do not see a method like hasChangesComparedTo(Bean bean).

1 Answers

Binder.hasChange is just a "dumb" tracker, whether any value has changed. It has not concept of a "previous or original value".

If you want to compare the state of the binder with an original bean, you would have to do that yourself:

  1. (optional): add a proper equals method to your bean, so the comparison is easier to handle
  2. keep the original value/bean and set a copy in the binder (e.g. use readBean(orig) and then writeBean(empty) to get the data into a fresh object)
  3. Compare the original and the copy and then react accordingly
Related