문제

I'm trying to access some objects inside a ui:binder component, but not sure of how to access the eventBus, requestFactory, etc without writing nasty code that will keep me awake at night (also take note that I'm completely new to JAVA, background is in Perl/Python/PHP).

My ui.xml file:

<ui:UiBinder xmlns:ui='urn:ui:com.google.gwt.uibinder'
    xmlns:g='urn:import:com.google.gwt.user.client.ui'
    xmlns:ig='urn:import:com.ig.client.scaffold.ui.widget'>
    <ui:style>
        ...
    </ui:style>
    <g:HorizontalPanel>
        ...
    </g:HorizontalPanel>
</ui:UiBinder>

Injecting the eventBus this way fails with com.ig.client.scaffold.ui.widget.R has no default (zero args) constructor.

public class R extends Composite {

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

    private final EventBus eventBus;

    @UiField SimplePanel fieldA, ...;

    @Inject
    public R(EventBus eventBus){
        this.eventBus = eventBus;
        initWidget(uiBinder.createAndBindUi(this));
    }
}

So, as per the error message, I create a UIFactory and then I get an error ... '{style.entityComponent}'> missing required attribute(s): eventBus Element ... (seems like it is trying to find the eventBus in the ui:binder stylesheet.

public class R extends Composite {

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

    private final EventBus eventBus;

    @UiField SimplePanel fieldA, ...;

    public @UiConstructor R(EventBus eventBus){
        this.eventBus = eventBus;
        initWidget(uiBinder.createAndBindUi(this));
    }

    @Inject
    @UiFactory R makeR(){
        return new R(eventBus);
    }
}

From reading and reading and more reading for the past couple of days, I haven't seen anyone accessing the eventBus, requestFactory and historyController directly in the view binding to the ui:binder widget which led to the conclusion that it's probably not a best practice anyway.

Let's say I have an oject, let's call it Proxy, proxy contains handleButtonClick which then calls eventBus.doSomething(). How do I link this Proxy object to the ui:binder widget without having to instantiate it or without having to pass it around to every widget?

Is it possible to do GIN injection on an interface and then implement that interface via class R which will then somehow contain the objects I'm looking for?

Any solution that work is welcome, just looking for a sample (that a Java n00b can understand) that will basically allow me to connect my front-end with the rest of the services created by ROO.

Thanks J

도움이 되었습니까?

해결책

Check out the Model, View, Presenter pattern - it solves this problem. Generally all non-display logic should be kept out of your views so that 1) the non-display logic can be unit tested without running inside a (slow to instantiate) browser and 2) different displays can be plugged into the same application without duplicating the non-display logic.

Here's an MVP example exhibiting the behavior you're looking for (note that the style is slightly different than the Activities & Places implementation).

MyPresenter.java:

public class MyPresenter {
  public interface Display extends IsWidget {
    void setButtonClickHandler(ClickHandler buttonClickHandler);
  }

  private final Display display;
  private final EventBus eventBus;

  @Inject
  public MyPresenter(EventBus eventBus,
                     Display display)
  {
    this.display = display;
    this.eventBus = eventBus;

    bind();
  }

  private void bind() {
    display.setButtonClickHandler(new ClickHandler() {
      @Override
      public void onClick(ClickEvent event) {
        eventBus.fireEvent(new MyButtonClickedEvent());
      }
    });
  }

  public void go(HasWidgets container) {
    container.add(display.asWidget());
  }
}

MyView.ui.xml:

<ui:UiBinder xmlns:ui='urn:ui:com.google.gwt.uibinder'
  xmlns:g='urn:import:com.google.gwt.user.client.ui'
  xmlns:ig='urn:import:com.ig.client.scaffold.ui.widget'>

  <g:Button ui:field="myButton"/>
</ui:UiBinder>

MyView.java

public class MyView extends Composite implements MyPresenter.Display {
  interface MyViewUiBinder extends UiBinder<Widget, MyView> {}
  private static MyViewUiBinder uiBinder = GWT.Create(MyViewUiBinder.class);

  private ClickHandler buttonClickHandler = null;

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

  @UiHandler("myButton")
  void onButtonClick(ClickEvent event) {
    if (buttonClickHandler != null) {
      buttonClickHandler.onClick(event);
    }
  }

  @Override
  public void setButtonClickHandler(ClickHandler buttonClickHandler) {
    this.buttonClickHandler = buttonClickHandler;
  }
}

And in your GIN module: bind(MyPresenter.Display.class).to(MyView.class);

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top