2013-04-18 65 views
3

我一直在使用ClientBundle和CssResources很長一段時間,但注意到我的樣式被注入很多次。我能指點的一件事是,我在我的模板中使用了不少<ui:with field='resources' type='com.vf.client.resources.Resources' />,我懷疑這是爲我的clientBundle創建多個副本。有一件事情甚至可能導致這種情況,我使用Ray Ryan的在我的clientfactory中緩存視圖的概念,以便在連接到DOM之前創建一些視圖。在我的基礎視圖中,我對資源使用了provided = true,希望不會讓UiBinder爲我生成一個新的。這可能不起作用,我懷疑和ui:正在創建一個新副本並忽略提供的= true。我在Chrome和Firebug中使用了開發人員工具來查看,並且在這兩種情況下,樣式都被注入很多次。如果不從我的所有UiBinder模板中刪除我的Resources類,甚至不知道如何解決此問題,這可能會導致相當多的工作。任何想法都表示讚賞。GWT,CssStyle被多次注入

/** 
* 
* @author chris 
*/ 
public abstract class ViewImpl extends Composite implements IView, RequiresResize { 

    @UiField(provided = true) 
    public final Resources resources; 

} 




public interface Resources extends ClientBundle, CellTable.Resources, CellList.Resources, DataGrid.Resources { 

    /** 
    * These are the obfuscated styles. 
    * 
    * @return 
    */ 
    @Source({ "default.css", "default-external.css"}) 
    public Style style(); 
} 

更新

我一直在使用一個工廠/單只是爲了確保它僅創建一個。在應用程序啓動時,我在ClientFactory實現中創建此資源ClientBundle。在我的應用程序開始時,我調用ensureEnjected對我的樣式,從那時起,ensureInjected永遠不會在我的代碼中調用。

這是我的工廠,只是得到我的單一請求工廠。我曾經在界面中使用了靜態初始化器,但是我回過頭來希望清理我的多個樣式問題。

import com.google.gwt.core.client.GWT; 

public class ResourcesFactory { 

    private static Resources instance = null; 

    private ResourcesFactory() { 
    } 

    public static final Resources getResources() { 
     if (instance == null) { 
      instance = GWT.create(Resources.class); 
     } 

     return instance; 
    } 
} 

我的客戶端軟件包已初始化並僅在此處注入。

@Override 
    public void onModuleLoad() { 
     if (Window.Location.getParameterMap().containsKey("debug")) { 
      Window.alert("Remote Debugging will be enabled, see server log for debug information"); 
      RemoteDebugService.enable();  
     } 

     try { 
      clientFactory = ClientFactory.INSTANCE; 
      masterLayoutPanel = clientFactory.getMasterLayoutPanel(); 
     } catch (Exception e) { 
      logger.log(Level.SEVERE, "Unable to instantiate the ClientFactory", e); 
      Window.alert("SOMEBODY BROKE THE BUILD, add ?debug to the url to enable remote debugging" + e.getMessage()); 
     } 

     RootLayoutPanel.get().add(masterLayoutPanel); 
     StyleInjector.inject(clientFactory.getResources().style().getText()); 

     PlaceHistoryHandler historyHandler = new PlaceHistoryHandler(clientFactory.getPlaceHistoryMapper()); 
     PlaceController placeController = clientFactory.getPlaceController(); 

     // Start PlaceHistoryHandler with our PlaceHistoryMapper 
     historyHandler.register(placeController, clientFactory.getEventBus(), defaultPlace); 

     startApplication(clientFactory, historyHandler); 
     } 

回答

0

我已經弄清楚至少有一個問題,我到目前爲止做錯了,導致我的風格被多次注入。首先,我將CellTable和Datagrid樣式定義在我的單個樣式表中,但注入此樣式時會多次注入。在下面的代碼中,default.css包含整個Web應用程序的所有CSS定義,包括單元格表格和單元格網格樣式。我正在注入這些應用程序開始當然cellTableStyle()和單元格dataGridStyle()注入整個樣式表。

public interface Resources extends ClientBundle, CellTable.Resources, CellList.Resources, DataGrid.Resources { 

... 

    /** 
    * {@link CellTable.Style} styles 
    */ 
    @Source("default.css") 
    Style cellTableStyle(); 

    /** 
    * {@link DataGrid.Style} styles 
    */ 
    @Source("default.css") 
    Style dataGridStyle(); 

    /** 
    * These are the obfuscated styles. 
    * 
    * @return 
    */ 
    @Source({ "default.css", "default-external.css" }) 
    public Style style(); 
... 


} 

如果聲明樣式這樣的樣式表應該被分解成不同的樣式表,只包含相關的樣式或實施我的主要風格,以實現這些接口,並刪除多餘的依賴風格的實現。

public interface Resources extends ClientBundle, CellTable.Resources, CellList.Resources, DataGrid.Resources { 

... 



    /** 
    * {@link CellTable.Style} styles 
    */ 
    @Source("celltable.css") 
    Style cellTableStyle(); 

    /** 
    * {@link DataGrid.Style} styles 
    */ 
    @Source("datagrid.css") 
    Style dataGridStyle(); 

    /** 
    * These are the obfuscated styles. 
    * 
    * @return 
    */ 
    @Source({ "default.css", "default-external.css" }) 
    public Style style(); 
... 


} 

這是可以將樣式傳遞給CellTable樣式並且樣式只會被注入一次的實現。請注意,樣式暗示了所有需要的表格,網格和列表樣式。

public interface Resources extends ClientBundle, CellTable.Resources, CellList.Resources, DataGrid.Resources { 

... 

public interface Style extends CssResource, ProgressWidget.Style, CellTable.Style, CellList.Style, DataGrid.Style { 
    ... 

} 


    /** 
    * Single style that can be used for all my cell table resources 
    * and the default styles. 
    * 
    * @return 
    */ 
    @Source({ "default.css", "default-external.css" }) 
    public Style style(); 
... 


} 
0

你是什麼意思與多次注射?

ClientBundle的基本上是Singleton's(請參閱此線程Will referring to a single ClientBundle class from multiple UiBinders cost anything?)。
因此,如果您使用provided=true或者不使用,那也不要緊。

但是,如果你真的想避免連ClientBundle代理,您可以使用GinFactory實例化(通過@InjectGWT.create或神奇)的ClientBundle一次,並將它注入到你的Views,但我想在編譯後的代碼,它贏得沒有太大區別。