GIN& GWT:将表示层与视图绑定

时间:2011-01-13 04:10:29

标签: gwt gin

我正在尝试将GWT视图与其表示层绑定,但它似乎没有做任何事情。

这是一个Spring Roo GWT生成的项目,我试图尽可能使用给出的脚手架。 视图是一个简单的按钮(R.ui.xml),视图的其余部分在R.java中定义:

public class R extends Composite implements RPresenter.Display {

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

    @UiField Button myButton;

    private ClickHandler buttonClickHandler = null;

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

    @UiHandler("myButton")
    void onButtonClick(ClickEvent event){
        GWT.log('Button clicked');
        if (buttonClickHandler != null){    
            GWT.log("buttonClickHandler event triggered");
            buttonClickHandler.onClick(event);
        }
    }

    @Override
    public void setButtonClickHandler(ClickHandler buttonClickHandler) {

        GWT.log("setButtonClickHandler");
        this.buttonClickHandler = buttonClickHandler;
    }

}

主持人:

public class RPresenter {

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

    private final Display display;
    private final EventBus eventBus;

    @Inject
    public RPresenter(EventBus eventBus, Display display){
        this.display = display;
        this.eventBus = eventBus;
        bind();
    }

    private void bind(){
        display.setButtonClickHandler(new ClickHandler() {
            @Override
            public void onClick(ClickEvent event) {
                GWT.log("onClick event triggered");
            }
        });
    }

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

}

对于我的GIN模块,我在... client.scaffold.ioc包中使用生成的ScaffoldModule:

public class ScaffoldModule extends AbstractGinModule {

    @Override
    protected void configure() {

        GWT.log("ScaffoldModule configure");

        bind(EventBus.class).to(SimpleEventBus.class).in(Singleton.class);
        bind(ApplicationRequestFactory.class).toProvider(RequestFactoryProvider.class).in(Singleton.class);
        bind(PlaceController.class).toProvider(PlaceControllerProvider.class).in(Singleton.class);

        //bind(RPresenter.Display.class).to(R.class).in(Singleton.class);
        bind(RPresenter.Display.class).to(R.class);
    }

    static class PlaceControllerProvider implements Provider<PlaceController> {

        private final EventBus eventBus;

        @Inject
        public PlaceControllerProvider(EventBus eventBus) {
            this.eventBus = eventBus;
        }

        public PlaceController get() {
            return new PlaceController(eventBus);
        }
    }

    static class RequestFactoryProvider implements Provider<ApplicationRequestFactory> {

        private final EventBus eventBus;

        @Inject
        public RequestFactoryProvider(EventBus eventBus) {
            this.eventBus = eventBus;
        }

        public ApplicationRequestFactory get() {
            ApplicationRequestFactory requestFactory = GWT.create(ApplicationRequestFactory.class);
            requestFactory.initialize(eventBus);
            return requestFactory;
        }
    }
}

在GWT开发模式控制台中,“ScaffoldModule configure”从不显示,但生成的脚手架似乎绑定得很好,因为事件从组件传递到组件没有任何障碍,除非绑定神奇地发生在其他地方和那是死代码。

当我把我的绑定(RPresenter.Display.class).to(R.class)放入时,它似乎没有进行绑定。我在GWT控制台中获得的唯一输出是“按钮单击”,在视图中调用,然后没有进一步。我显然错过了什么,有什么想法吗?

1 个答案:

答案 0 :(得分:1)

对GWT.log()的调用不会从AbstractGinModule输出任何内容 - gin at compile time使用扩展AbstractGinModule(在您的情况下为ScaffoldModule)的类来决定注入接口使用哪些具体实现。从您的描述的其余部分(即UI显示在应用程序中),您的依赖注入似乎正常工作。