javagwtmvpgwt-editors

GWT Editors framework - ListEditor, removing items, MVP violation


public class PersonListEditor extends Composite implements IsEditor<ListEditor<Person, PersonListItemWidget>> {
    private static PersonListEditorUiBinder uiBinder = GWT.create(PersonListEditorUiBinder.class);
    interface PersonListEditorUiBinder extends UiBinder<Widget, PersonListEditor> {}

    private class Source extends EditorSource<PersonListItemWidget> {
        @Override
        public PersonListItemWidget create(int index) {
            PersonListItemWidget widget = new PersonListItemWidget();
            panel.insert(widget, index);
            return widget;
        }                   
    }   

    @UiField VerticalPanel panel;
    private ListEditor<Person, PersonListItemWidget> editor = ListEditor.of(new Source());

    public PersonListEditor() {
        initWidget(uiBinder.createAndBindUi(this));
    }

    @Override
    public ListEditor<Person, PersonListItemWidget> asEditor() {
        return editor;
    }
}

PersonListItemWidget has a Delete button and when this button is clicked, I need to remove the related item from the list.

  1. I can make PersonListEditor listen item widget's notifications (like "my delete button is clicked"), but in this case, I'll only have a reference to the widget and not a real Person object that I need in fact. I may also add some logic to get related widget's index from the list of panel items and then get Person object by that index, but that looks awful.

  2. I can make my PersonListItemWidget to be a ValueAwareEditor, so each widget will know its Person, but the whole idea of ValueAwareEditor looks like MVP violation for me since Google says that View layer shouldn't be aware of model and it should only be "buttons" and "labels".

What's the right approach here?


Solution

  • Either approach is fine.

    MVP is not set in stone (it's not even defined; it was coined by Martin Fowler but he retired the term in favor of two more specific patterns), so you're only violating the rules you gave to yourself. Put differently, the Editor framework as a whole can be seen as violating MVP: each editor know the model, not necessarily the exact instance it's editing (as with ValueAwareEditor or LeafValue), but at least the kind of objects it's an editor of.

    FYI, we do it using indexes. It matters more that it's guaranteed to work than that it "looks good" (even though it's obviously better if it also looks good).