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 开发者_StackOverflowmy 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);
}
}
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
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
精彩评论