问题
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.
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 realPerson
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 getPerson
object by that index, but that looks awful.I can make my
PersonListItemWidget
to be aValueAwareEditor
, so each widget will know itsPerson
, but the whole idea ofValueAwareEditor
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