With GWT, is there a way to not load widgets declared in uibinder xml files?
One common design I have with GWT is to create a widget which contains two children: A and B.
I declare these two widgets A and B in the uibinder file associated to my main widget.
What I want to do is to load or not widget A depending on an if statement.
Ideal approach is to set provided=true for widget A and to set widget A to null when I want to not load this widget. But GWT throws an error.
Is there a way to declare widgets in uibinder and then not loading them ?
thanks
EDIT: after a lot of discussions, an ideal approach is to declared a field "provided=true" and "optional=true" when optional=true, createAndBindUI must not throw an Excepti开发者_开发知识库on if the field is null. This is a clean approach.
If you think that this feature must exists in GWT, please star this issue: http://code.google.com/p/google-web-toolkit/issues/detail?id=5699
EDIT 2 : using the LazyPanel as described by Thomas seems to be a better way to handle this.
I stumbled on the GWT issue, which led me here, so here's my take on it, 20 months later.
Use a LazyPanel
and set it to visible="false"
so that it's content is not built until you need it (simply call setVisible(true)
to reveal it, triggerring the lazy-initialization of its content.
LazyPanel
is fully integrated with UiBinder so that you declare its content in the same UiBinder template, as if it were a SimplePanel
, without even bothering creating a subclass of LazyPanel
. See https://developers.google.com/web-toolkit/doc/2.4/DevGuideUiBinder#Lazy
I would revert the logic. If you don't need the widget remove it
widget.removeFromParent();
I think it's a cleaner approach, since the UIBinder automatically defines and creates them.
Since conditionals are not allowed in a UiBinder XML I'd suggest you define placeholders (i.e., panels) in your ui.xml
, have a reference to them in your view class and decide there whether a specific widget needs to be created and added or not.
This is a common approach when using MVP pattern with nested presenters.
EDIT:
Until your request has been implemented by the GWT developers you could extend the FlowPanel
and overwrite the add(Widget)
method to check for null
as in:
public class ExtendedFlowPanel extends FlowPanel {
public ExtendedFlowPanel() {
super();
}
@Override
public void add(Widget w) {
if (w != null) {
super.add(w);
}
}
}
This way you can use provided = true
and pass null
to the panel.
精彩评论