如何在 gwt-platform 中使用 GWT 的编辑器框架? [英] How to use GWT's Editor Framework with gwt-platform?

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

问题描述

我正在使用 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...

目前我尝试过但没有成功:

At the moment I tried this without success:

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 的编辑器框架最接近 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 创造的,但他弃用了该术语,转而支持两种更具体的模式),所以你只是违反了你给自己的规则.把不同的是,编辑器框架作为一个整体可以被视为违反 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 的编辑器框架?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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