GWT& GIN:如何使用 UIBinder 将 PlaceController 注入到小部件中

发布于 2024-10-12 20:27:17 字数 720 浏览 8 评论 0原文

我有一个在 UIBinder 中使用的 InlineLabel 子类。如何通过 GIN 将 PlaceController 注入其中,以便该小部件可以在 UIBinder 中使用? (回想一下,UIBinder 需要一个无参数构造函数。)

如果这是不可能的,那么使 PlaceController 可用于小部件以便小部件可以在 onClick() 事件期间使用它的最简洁方法是什么?

编辑:

在这种情况下,我不确定 MVP 是否真的是最佳解决方案(不过,我很高兴您改变主意。)

我将在 UIBinder 中声明数十个这样的 InlineLabel 实例foo.ui.xml 文件。如果我实现 MVP,这意味着将每个实例声明为视图中的 @UiField 成员。当我有这么多的时候,这变得相当笨拙。这就是为什么我希望以半自动的方式将 PlaceController 注入每个 InlineLabels 中,并避免必须手动将它们连接到视图中。

如果有一种方法可以将演示者注入到每个 InlineLabels 中,那么委托可以执行如下操作:

   public class MyInlineLabelSubclass {
       // ...

       public void onClick(ClickEvent event)
       {
         presenter.labelClicked(this);
       }
    }

I have an InlineLabel subclass that I used in UIBinder. How can I inject a PlaceController into it via GIN, so that the widget can be used in UIBinder? (recall that UIBinder requires a no-args constructor.)

If that's not possible, what's the cleanest way to make a PlaceController available to a widget so that it can be used by the widget during onClick() events?

Edit:

I'm not sure MVP is really the best solution in this case (I'm happy to have you change my mind, however.)

I will have dozens of these InlineLabel instances declared in my UIBinder foo.ui.xml file. If I implement MVP, that means declaring each of these instances as @UiField members in the view. That gets rather unwieldy when I have so many of them. That's why I was hoping to inject the PlaceController into each of the InlineLabels in a semi-automated way, and avoid having to manually wire them into the view.

It would also be acceptable if there were a way to inject the presenter into each of the InlineLabels... then the delegation could be done something like:

   public class MyInlineLabelSubclass {
       // ...

       public void onClick(ClickEvent event)
       {
         presenter.labelClicked(this);
       }
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

£烟消云散 2024-10-19 20:27:17

您可以使用 @UiHandler 注释将处理程序添加到 UiBinder 元素,而无需使用 @UiField 引用:

<ui:UiBinder>
  <g:InlineLabel ui:field="name"/>
  <g:InlineLabel ui:field="address"/>
  <g:InlineLabel ui:field="zipCode"/>
</ui:UiBinder>

并且在视图中:

@UiHandler({"name","address","zipCode"})
void onClick(ClickEvent event) {
  // Source will be one of the three InlineLabels.
  presenter.labelClicked(event.getSource());
}

不要为您的小部件提供直接句柄到 PlaceController - 委托给视图的 Presenter(通过回调或 Presenter 接口)。请参阅 http://code.google.com/webtoolkit/doc/latest/ DevGuideMvpActivitiesAndPlaces.html#Views

You can use the @UiHandler annotation to add a handler to a UiBinder element without having a @UiField reference:

<ui:UiBinder>
  <g:InlineLabel ui:field="name"/>
  <g:InlineLabel ui:field="address"/>
  <g:InlineLabel ui:field="zipCode"/>
</ui:UiBinder>

And in the view:

@UiHandler({"name","address","zipCode"})
void onClick(ClickEvent event) {
  // Source will be one of the three InlineLabels.
  presenter.labelClicked(event.getSource());
}

Don't give your widget a direct handle to a PlaceController - delegate to the view's Presenter (via. a callback or a Presenter interface). See http://code.google.com/webtoolkit/doc/latest/DevGuideMvpActivitiesAndPlaces.html#Views

弥枳 2024-10-19 20:27:17

我在网上看到过一个示例,其中超链接是直接从 PlaceController 使用的名称标记构建的。例如。

<!DOCTYPE ui:UiBinder SYSTEM "http://dl.google.com/gwt/DTD/xhtml.ent">
<ui:UiBinder xmlns:ui="urn:ui:com.google.gwt.uibinder"
    xmlns:g="urn:import:com.google.gwt.user.client.ui">
  <ui:with type="com.gwtplatform.samples.nested.client.NameTokens" field="nameTokens"></ui:with>
  <g:HTMLPanel>
    <g:InlineHyperlink targetHistoryToken="{nameTokens.getHomePage}">Home</g:InlineHyperlink> | 
    <g:InlineHyperlink targetHistoryToken="{nameTokens.getAboutUsPage}">About Us</g:InlineHyperlink> | 
    <g:InlineHyperlink targetHistoryToken="{nameTokens.getContactPage}">Contact</g:InlineHyperlink>
  </g:HTMLPanel>
</ui:UiBinder>

有关此示例的更多信息,请参阅此处:

http://code.google .com/p/gwt-platform/wiki/SimpleNestedSample#Step_3:_Creating_a_custom_widget

I've seen an example online where hyperlinks were built directly from the name tokens used by the PlaceController. eg.

<!DOCTYPE ui:UiBinder SYSTEM "http://dl.google.com/gwt/DTD/xhtml.ent">
<ui:UiBinder xmlns:ui="urn:ui:com.google.gwt.uibinder"
    xmlns:g="urn:import:com.google.gwt.user.client.ui">
  <ui:with type="com.gwtplatform.samples.nested.client.NameTokens" field="nameTokens"></ui:with>
  <g:HTMLPanel>
    <g:InlineHyperlink targetHistoryToken="{nameTokens.getHomePage}">Home</g:InlineHyperlink> | 
    <g:InlineHyperlink targetHistoryToken="{nameTokens.getAboutUsPage}">About Us</g:InlineHyperlink> | 
    <g:InlineHyperlink targetHistoryToken="{nameTokens.getContactPage}">Contact</g:InlineHyperlink>
  </g:HTMLPanel>
</ui:UiBinder>

See here for more on this example:

http://code.google.com/p/gwt-platform/wiki/SimpleNestedSample#Step_3:_Creating_a_custom_widget

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文