1

Our application uses GIN to instantiate its GWT Resource bundle dynamically. This is done to enable loading different stylesheets at runtime for different display modes. In general, this means that I can create the singleton instance of the resources using something like MyApp.ginjector.getResources() as opposed to the standard GWT.create(MyAppResources.class).

This works well for everything but UiBinder code. We have been using <ui:with ref="res" type="com.company.MyAppResources" /> and I would like the instance of MyAppResources to be created with GIN rather than GWT.create.

According to the GWT UiBinder docs, there are two ways to handle this. Either use a @UiField(provided=true) or a @UiFactory in the component. Both of these methods are undesirable because they require people on my team (including myself) to remember to put this boilerplate code into every widget. Worse, if the creation code is omitted, everything will still appear to work, since GWT.create will create an instance of our Resources, just not the one we want.

I am looking for a method of creating some sort of global @UiFactory method which all my components will use, which will delegate to GIN. Alternatively, some way of hooking into GWT.create for specific classes and having client-side runtime code create the instance would work as well, as I could then delegate the object creation to my static Ginjector.

Yona Appletree
  • 8,801
  • 6
  • 35
  • 52

1 Answers1

3

If you have a com.company.MyApp with a static field ginjector, then you can use:

<ui:import field="com.company.MyApp.ginjector" />

and then (assuming ginjector.getResources().style().foo() in Java):

<span class="{ginjector.getResources.style.foo}">
Thomas Broyer
  • 64,353
  • 7
  • 91
  • 164
  • Oh, that's clever, I haven't used ui:import before. For cleanliness, you could create a class with the express purpose of providing styles and other static resources to UiBinder files. – Yona Appletree Oct 12 '12 at 23:30
  • Another solution I thought of is to create a common base class for our widgets, (e.g. MyAppComposite), which can provide UiFactory methods for resources or potentially widgets, if they need to be created specially. – Yona Appletree Oct 12 '12 at 23:32
  • Has been replaced with ? I am using GWT 2.5.1 and I got it to work with – Ninju Bohra May 02 '14 at 14:36
  • No. `` is about setting a (possibly implicit) `@UiField` with a `GWT.create()`d instance of a given type (or just using such a field from XML when it's a `@UiField(provided=true)`). `` is about accessing the value of a static field (constant) in a given class. – Thomas Broyer May 02 '14 at 22:15