GWT编辑框架 - ListEditor,删除项目,MVP违规 [英] GWT Editors framework - ListEditor, removing items, MVP violation

查看:95
本文介绍了GWT编辑框架 - ListEditor,删除项目,MVP违规的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  public class PersonListEditor扩展Composite implements IsEditor< ListEditor< Person,PersonListItemWidget>> {
private static PersonListEditorUiBinder uiBinder = GWT.create(PersonListEditorUiBinder.class);
接口PersonListEditorUiBinder扩展了UiBinder< Widget,PersonListEditor> {}

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

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

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

@Override
public ListEditor< Person,PersonListItemWidget> asEditor(){
返回编辑器;


code
$ b preersonListItemWidget
有一个删除按钮,当这个按钮被点击时,我需要从列表中删除相关的项目。


  1. 我可以让 PersonListEditor 删除按钮被点击),但在这种情况下,我只会引用这个小部件,而不是真正需要的 Person 对象。我也可以添加一些逻辑从面板项目列表中获取相关的小部件索引,然后通过该索引得到<$​​ c $ c> Person 对象,但看起来很糟糕。


  2. 我可以让我的 PersonListItemWidget 成为 ValueAwareEditor ,因此每个小部件会知道它的 Person ,但是对于我来说, ValueAwareEditor 的整个想法看起来像MVP违反,因为Google说View层不应该意识到模型,它应该只是按钮和标签。

  3. 方法在这里?

    解决方案

    任何一种方法都可以。

    不是一成不变的(它甚至没有被定义;它是由Martin Fowler创造的,但他退役了术语支持两种更具体的模式),所以你只是违反了你给自己的规则。换句话说,编辑器框架作为一个整体可以被视为违反MVP:每个编辑器都知道模型,不一定是编辑的确切实例(与 ValueAwareEditor LeafValue ),但至少是它的编辑器对象的类型。



    仅供参考用索引来做。更重要的是,有保证的工作比看起来不错(尽管如果看起来更好的话明显更好)。


    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?

    解决方案

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

    这篇关于GWT编辑框架 - ListEditor,删除项目,MVP违规的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆