gpt4 book ai didi

gwt - 如何将 GWT 的 Editor Framework 与 gwt-platform 一起使用?

转载 作者:行者123 更新时间:2023-12-04 07:26:09 26 4
gpt4 key购买 nike

我正在使用 gwt-platform 并尝试实现 GWT 的编辑器框架。但我不能从演示者内部得到它。网上有一些答案,说我必须以某种方式将 EditorDriver 注入(inject) Presenter,但我不知道该怎么做......

目前我尝试了这个但没有成功:

public class MyPresenter extends Presenter<MyPresenter.MyView, MyPresenter.MyProxy> implements MyUiHandlers {
public interface MyView extends View, HasUiHandlers<MyUiHandlers>, Editor<MyModel> {}

@ProxyStandard
@NameToken(NameTokens.myPage)
@NoGatekeeper
public interface MyProxy extends ProxyPlace<MyPresenter> {}

interface Driver extends SimpleBeanEditorDriver<MyModel, MyView> {}
private Driver editorDriver;
DispatchAsync dispatcher;

@Inject
public MyPresenter(EventBus eventBus, MyView view, MyProxy proxy, DispatchAsync dispatcher) {
super(eventBus, view, proxy);
getView().setUiHandlers(this);
this.dispatcher = dispatcher;

MyModel m = new MyModel();
m.setId(1L);
m.setUsername("username");
m.setPassword("password");

editorDriver = GWT.create(Driver.class);
editorDriver.initialize(this.getView());
editorDriver.edit(m);
}

...
}

如果我明确指定 ViewImplementation,它就可以工作,但这不是 MVP 应该工作的方式:
interface Driver extends SimpleBeanEditorDriver<MyModel, MyViewImpl> {}

...

editorDriver.initialize((MyViewImpl) this.getView());

如果有人能给我一个如何做正确的例子,我会很好。

谢谢

最佳答案

一种类似于 Expenses sample 早期版本中使用的方法。为我工作:

View 应该实现的接口(interface)。使用通配符是为了让演示者不需要知 Prop 体的 View 实现:

import com.google.gwt.editor.client.Editor;
import com.gwtplatform.mvp.client.View;

/**
* Implemented by views that edit beans.
*
* @param <B> the type of the bean
*/
public interface BeanEditView<B> extends View, Editor<B> {

/**
* @return a new {@link SimpleBeanEditorDriver} initialized to run
* this editor
*/
SimpleBeanEditorDriver<B, ?> createEditorDriver();
}

您的演示者现在应该看起来像这样:
public class MyPresenter extends Presenter<MyPresenter.MyView, MyPresenter.MyProxy> implements MyUiHandlers {
public interface MyView extends BeanEditView<MyModel>, HasUiHandlers<MyUiHandlers> {}

@ProxyStandard
@NameToken(NameTokens.myPage)
@NoGatekeeper
public interface MyProxy extends ProxyPlace<MyPresenter> {}

private SimpleBeanEditorDriver<MyModel, ?> editorDriver;
DispatchAsync dispatcher;

@Inject
public MyPresenter(EventBus eventBus, MyView view, MyProxy proxy, DispatchAsync dispatcher) {
super(eventBus, view, proxy);
getView().setUiHandlers(this);
this.dispatcher = dispatcher;

MyModel m = new MyModel();
m.setId(1L);
m.setUsername("username");
m.setPassword("password");

editorDriver = getView().createEditorDriver();
}

...
}

以及 View 实现:
public class MyViewImpl extends ViewWithUiHandlers<MyUiHandlers> implements
MyPresenter.MyView {

public interface Binder extends UiBinder<Widget, MyViewImpl> { }
private static Binder uiBinder = GWT.create(Binder.class);

/**
* The driver to link the proxy bean with the view.
*/
public interface EditorDriver extends SimpleBeanEditorDriver<MyModel, MyViewImpl> { }

private final Widget widget;

public MyViewImpl() {
widget = uiBinder.createAndBindUi(this);
}

@Override
public SimpleBeanEditorDriver<MyModel, ?> createEditorDriver() {
EditorDriver driver = GWT.create(EditorDriver.class);
driver.initialize(this);
return driver;
}

@Override
public Widget asWidget() {
return widget;
}

...
}

这与我使用 GWT 的编辑器框架所能达到的 MVP 一样接近。我找不到让 View 实现不知道模型的方法,但我认为这不是真的必要。

如果有人对此有任何改进,我很高兴听到。

在 GWT Editors 上找到了一些额外的评论。似乎不可能完全分离模型。正如 Thomas Broyer 所说的 his answer另一个编辑问题:

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

关于gwt - 如何将 GWT 的 Editor Framework 与 gwt-platform 一起使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10694589/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com