2012-02-13 64 views
4

我想在MVP GWT 2.4中使用杜松子酒。在我的模塊中,我有:在Gwt 2.4 EventBus和杜松子酒的麻煩

import com.google.web.bindery.event.shared.EventBus; 
import com.google.web.bindery.event.shared.SimpleEventBus; 

    @Override 
     protected void configure() { 
     bind(EventBus.class).to(SimpleEventBus.class).in(Singleton.class); 
     ... 
     } 

上面的代碼使用新的com.google.web.bindery.event.shared.EventBus。問題是當我想要實現的活動注入事件總線MVP活動:

package com.google.gwt.activity.shared; 

import com.google.gwt.event.shared.EventBus; 
import com.google.gwt.user.client.ui.AcceptsOneWidget; 

public interface Activity { 

    ... 

    void start(AcceptsOneWidget panel, EventBus eventBus); 
} 

Activity使用過時的com.google.gwt.event.shared.EventBus。我怎樣才能調和這兩個?很明顯,如果我要求使用不推薦使用的EventBus類型,那麼Gin會抱怨,因爲我沒有爲它指定綁定。

更新:這將允許應用程序建立的,但現在有兩個不同的EventBus s,這是可怕的:

protected void configure() { 
    bind(com.google.gwt.event.shared.EventBus.class).to(
     com.google.gwt.event.shared.SimpleEventBus.class).in(Singleton.class); 
    bind(EventBus.class).to(SimpleEventBus.class).in(Singleton.class); 
    ... 
+0

一個黑客是隻使用了過時的版本無處不在我的代碼。這樣做有多糟糕? – 2012-02-13 21:54:01

回答

2

我問了一個類似的問題:Which GWT EventBus should I use?

你不需要過時的事件總線,因爲它擴展了WebBindery之一。

創建基礎的活動,你的活動都與此代碼擴展:

// Forward to the web.bindery EventBus instead 
@Override 
@Deprecated 
public void start(AcceptsOneWidget panel, com.google.gwt.event.shared.EventBus eventBus) { 
    start(panel, (EventBus)eventBus); 
} 

public abstract void start(AcceptsOneWidget panel, EventBus eventBus); 
0

我發現,無論是新老版本綁定到同一個實例幫助。

/* 
    * bind both versions of EventBus to the same single instance of the 
    * SimpleEventBus 
    */ 
    bind(SimpleEventBus.class).in(Singleton.class); 
    bind(EventBus.class).to(SimpleEventBus.class); 
    bind(com.google.gwt.event.shared.EventBus.class).to(SimpleEventBus.class); 

現在,只要你的代碼需要一個EventBus注入的代碼需要一個,避免廢棄警告。

1

我認爲一個更清潔的解決方案是這樣的:

public class GinClientModule extends AbstractGinModule { 

    @Override 
    protected void configure() { 
     bind(EventBus.class).to(SimpleEventBus.class).in(Singleton.class); 
     ... 
    } 

    @Provides 
    @Singleton 
    public com.google.gwt.event.shared.EventBus adjustEventBus(
      EventBus busBindery) { 
     return (com.google.gwt.event.shared.EventBus) busBindery; 
    } 

... 

請參閱我的回答here