简体   繁体   English

GWT编辑器框架-ListEditor,删除项目,违反MVP

[英]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. PersonListItemWidget有一个“删除”按钮,单击此按钮时,我需要从列表中删除相关项目。

  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. 我可以使PersonListEditor侦听项目窗口小部件的通知(例如“单击我的删除按钮”),但是在这种情况下,我将仅具有对该窗口小部件的引用,而实际上没有真正的Person对象。 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. 我还可以添加一些逻辑,以从面板项目列表中获取相关小部件的索引,然后通过该索引获取Person对象,但这看起来很糟糕。

  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". 我可以将PersonListItemWidget设置为ValueAwareEditor ,这样每个小部件都将知道其Person ,但是ValueAwareEditor的整个想法对我来说似乎违反了MVP,因为Google表示View层不应该知道模型,而应该只是“按钮” ”和“标签”。

What's the right approach here? 这里正确的方法是什么?

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. MVP并不是一成不变的(甚至没有定义;它是由Martin Fowler创造的,但是他退休了 ,而是使用了两个更具体的模式),所以您只违反了您给自己的规则。 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. 换句话说,Editor框架作为一个整体可以视为违反了MVP:每个编辑器都知道模型,不一定知道它正在编辑的确切实例(例如ValueAwareEditorLeafValue ),但至少知道它是编辑器的那种对象。

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). 它更重要的是它保证比工作,它“看起来不错”(即使它显然更好,如果它看起来不错)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM