我一直在使用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);
}