Ban*_*der 6 java gwt dom widget
我正在阅读GWT准备我的第一个GWT应用程序,并偶然发现了LazyDomElement并发现它很有趣.
我的理解是,当你真正想要创建自己的Widget子类(而不仅仅是简单扩展Composite)时,你需要做各种额外的工作来将Widget与DOM连接起来.
所以我要问:你需要做的"额外工作"是什么 - 你(基本上)从子类化中获得免费Composite- 你如何使用a LazyDomElement来使这更容易或提升性能?
从 GWT 文档和源代码来看,此类似乎仅与UIBinder功能有关,并且没有任何基本 GWT 小部件使用此类。\n其主要也是唯一的功能LazyDomElement是延迟访问您的小部件的字段。假设您有一个带有模板的小部件:
<gwt:HTMLPanel>\n  <div ui:field="lazyField" />\n  <div ui:field="generalField" /> \n  <!-- Other fields -->\n</gwt:HTMLPanel>\nRun Code Online (Sandbox Code Playgroud)\n\n和它的 Java 类:
\n\npublic class MyCoolWidget extends UIObject {\n\xc2\xa0 interface MyUiBinder extends UiBinder<DivElement, MyCoolWidget> {}\n\xc2\xa0 private static MyUiBinder uiBinder = GWT.create(MyUiBinder.class);\n\n\xc2\xa0 @UiField LazyDomElement<DivElement> lazyField;\n\n  @UiField DivElement generalField;\n\n  // all other fields \xe2\x80\xa6\n\n\xc2\xa0 public MyCoolWidget() {\n\xc2\xa0 \xc2\xa0 // Initializes all your fields and also calls \'getElementById\' for all\n    // not lazy fields of your widgets to have a reference to them.\n    // There could be hundreds of them if you are building really cool app, \n    // and also they could be hidden (e.g. other tab on UI) and not required \n    // to be accessed at all for some cases.\n\xc2\xa0 \xc2\xa0 setElement(uiBinder.createAndBindUi(this));\n\xc2\xa0 }\n\n\xc2\xa0 public void setLazyField(String value) { \n    // Finally we need to modify the field, ok, \n    // we access the DOM only at this moment\n    // (please take a look at \'get()\' method implementation for details)  \n    lazyField.get().setInnerText(name); \n  }\n\n  public void setGeneralField(String value) { \n    // Reference to element is already there, we are just going  \n    // to change it\'s property\n    generalField.setInnerText(name); \n  } \n}\nRun Code Online (Sandbox Code Playgroud)\n\n因此,使用它的原因仅取决于您的应用程序的特定情况,您是否需要延迟加载小部件元素。
\n\nUPD。值得一提的是,我还没有在实际项目中使用过这个类:)。我可以想象一些场景。例如,您需要构建一个门票预订面板。具有以下初始要求:
\n\n因此,您需要一次渲染最多 10 个相同的丰富表单,而无需在加载页面之前访问它们的字段。我们可以构建ReservationForm小部件(ReservationForm.ui.xml带有标记和ReservationForm.java一些逻辑)并用于LazyDomElement输入字段以节省我们的首次加载时间。