viewinjectgwtp

GWTP - Inject widget in another view


I'm working on a project that uses GWTP and I was wondering if I can inject a view's object into another view. something like:

public class BarView extends ViewWithUiHandlers<BarUiHandlers> implements BarPresenter.BarView {    
    public interface Binder extends UiBinder<HTMLPanel, BarView> {
    }

    @UiField SomeWidget someWidget;

    @Inject
    public PlayerView(Binder binder) {
        initWidget(binder.createAndBindUi(this));
    }
}

public class FooView extends ViewWithUiHandlers<FooUiHandlers> implements FooPresenter.FooView {    
    public interface Binder extends UiBinder<HTMLPanel, FooView> {
    }

    SomeWidget someWidget;

    @Inject
    public PlayerView(Binder binder, SomeWidget someWidget) {
        initWidget(binder.createAndBindUi(this));
        this.someWidget = someWidget;
    }
}

Would this work? does anybody know how to accomplish that?


Solution

  • Yes it can be done!

    Bind your SomeWidget class in Singleton:

    bind(SomeWidget.class).in(Singleton.class);
    

    so both views will have the same SomeWidget instance.

    If your SomeWidget is used in UiBinder (like in BarView), you must annotate your SomeWidget field with @UiField(provided = true):

    @UiField(provided = true)
    SomeWidget someWidget;
    

    and assign the field in your constructor before the call to initWidget:

    @Inject
    public PlayerView(Binder binder, SomeWidget someWidget) {
        this.someWidget = someWidget;
        initWidget(binder.createAndBindUi(this));
    }
    

    You must also do those 2 tricks in FooView.