GWT Editors framework - ListEditor, removing items, MVP violation

人盡茶涼 提交于 2019-12-09 13:30:14

问题


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?


回答1:


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



来源:https://stackoverflow.com/questions/7914146/gwt-editors-framework-listeditor-removing-items-mvp-violation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!