Revert (Undo) implementation in GWT

2019-04-11 14:43发布

问题:

We are trying to build a GUI framework using GWT. We are finding it hard to implement the cancel functionality in the framework.

Required feature is this:

We have CRUD screens which have pop-ups, grids and so on. When the user changes anything in the GUI and then clicks on cancel() he should be given a notification message saying that something has changed.

Approach that we have tried:

Currently we are trying to keep a hashmap of key vs value of the entire pojo object and trying to compare it against the model which gets updated as and when user changes something. But this is adding lot of unwanted code in every pojo and not working as expected when user adds data directly from the backend.

Is there any elegant way in achieving this functionality? Kindly note that *we are not using Editor framework of GWT *(https://developers.google.com/web-toolkit/doc/latest/DevGuideUiEditors) in our application.

Example: Suppose I have a pojo like this:

public class Person {

    List<Address> address;
    PhoneNumber phoneData;


    // and so on along with getters and setters

}

How will I write a generic clone method for this? And even if I manage to do that somehow that will lead to lot of code in every pojo (our application has hundreds of them) which doesn't seem right.

Please note that, our pojo gets updated as soon as something is changed in GUI to achieve live binding.

回答1:

So you have "Save" and "Cancel" buttons in your form?

I would recommend you to change the concept. Update your object properties immediately as user edit them (as in GMail, JIRA and many other modern applications) in an OnChange event handler.

Save all updates to the session stack as UpdateAction objects and let the user undo every single property modification calling UpdateAction.undo() method.

The benefits are:

  1. this design is much more user friendly than "Click "Edit" - update - click "Save"" scenario.

  2. You don't need separate view/edit forms/popup dialogs - just a single form for both viewing and editing.