gpt4 book ai didi

GWT,CssStyle被多次注入(inject)

转载 作者:行者123 更新时间:2023-12-02 05:01:17 24 4
gpt4 key购买 nike

我使用 ClientBundle 和 CssResources 已经有一段时间了,但我注意到我的样式被注入(inject)了很多次。我也可以指出的一件事是我使用了很多 <ui:with field='resources' type='com.vf.client.resources.Resources' />在我的模板中,我怀疑这是在创建我的 clientBundle 的多个副本。甚至可能导致这种情况的一件事是,我使用 Ray Ryan 的概念在我的客户端工厂中缓存我的 View ,因此一些 View 是在附加到 DOM 之前创建的。在我的基本 View 中,我在资源上使用了 provided=true 以便希望不让 UiBinder 为我生成一个新资源。虽然这可能不起作用,但我怀疑 ui:with 正在创建一个新副本并忽略提供的 = true。我已经使用 Chrome 和 Firebug 中的开发人员工具来查看,在这两种情况下,样式都被注入(inject)了很多次。如果不从我的所有 UiBinder 模板中删除我的 Resources 类,我不确定如何解决这个问题,这会带来相当多的工作。任何想法表示赞赏。

/**
*
* @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 实现中创建了这个 Resources 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;
}
}

我的客户端包仅在此处初始化和注入(inject)。

  @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);
}

最佳答案

多次注入(inject)到底是什么意思?

ClientBundle 基本上是 Singleton 的(请参阅此线程 Will referring to a single ClientBundle class from multiple UiBinders cost anything?)。
因此,您是否使用 provided=true 甚至都无关紧要。

但如果你真的想避免 ClientBundle 代理,你可以使用 GinFactory 来实例化(GWT.create 或神奇地通过 @Inject ) ClientBundle 一次并将其注入(inject)到您的 Views 但我猜在编译后的代码中它不会做太多区别。

关于GWT,CssStyle被多次注入(inject),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16086848/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com