如何在gwt-platform上使用GWT的Editor Framework? [英] How to use GWT's Editor Framework with gwt-platform?

查看:91
本文介绍了如何在gwt-platform上使用GWT的Editor Framework?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm using gwt-platform and tried to implement GWT's editor framework. But I don't get it working from within the presenter. There are some answers around the web, that say I have to inject the EditorDriver somehow into the Presenter, but I don't know how to do this...

此刻,我尝试失败了:

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的工作方式:

It works if I explicitly specify the ViewImplementation, but that's not the way MVP should work:

interface Driver extends SimpleBeanEditorDriver<MyModel, MyViewImpl> {}

...

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

如果有人能给我一个正确的做法示例,我会很好.

I would be nice if someone could give me an example how to do it right.

谢谢

推荐答案

与早期版本的

An approach similar to what was used in an earlier version of the Expenses sample worked for me:

视图应实现的接口.使用通配符使演示者无需了解具体的视图实现:

An interface that the view should implement. The wildcard is used so that the presenter does not need to know the concrete view implementation:

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();
}

您的演示者现在应该看起来像这样:

Your presenter should look something like this now:

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();
    }

    ...
}

视图实现:

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的Editor Framework获得MVP差不多.我找不到视图实现不知道模型的方法,但我认为这不是真的必要.

That's as close as I could get to MVP with GWT's Editor Framework. I couldn't find a way for the view implementation to NOT know the model but I don't think it's really necessary.

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

If anyone has any improvements on this, I'm glad to hear.

在GWT编辑器上找到了一些其他注释.似乎完全不可能完全分离模型.正如Thomas Broyer在他对另一个编辑器问题的回答中所述:

Found some additional comments on GWT Editors. It seems that it might just not be possible to completely separate the model. As Thomas Broyer puts it in his answer to another Editor question:

"MVP并不是一成不变的(甚至没有定义;它是Martin Fowler创造的,但是他退休了,而是使用了两种更具体的模式),所以您只是在违反自己赋予的规则.不同的是,整个Editor框架可以看作违反了MVP:每个编辑器都知道模型,不一定知道它正在编辑的确切实例(例如ValueAwareEditor或LeafValue),但至少知道它是编辑器的对象类型."

"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-platform上使用GWT的Editor Framework?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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