1

I want to be able to achieve the following using guice/gin:

  1. Get all sort of constants from the server (user settings, language, etc)
  2. Bind those constants to "Named(###)" in a guice/gin module
  3. inject those to constructors in my code, that are called only after I init the 2 steps above.

Can I do this somehow? if so, how?

Shuky Capon
  • 785
  • 5
  • 22
  • 2
    [What have you tried?](http://www.whathaveyoutried.com/) – David B Jun 20 '12 at 13:32
  • @DavidB, today I'm solving this by injecting a "Settings" object using a provider to any other object that needs the constants, but I rather inject Named() primitives for the constants I need and I can't figure out a way to do this (since I install my gin module and only sometime after retrieve the settings). – Shuky Capon Jun 20 '12 at 15:57

1 Answers1

2

There are basically two ways to get data from your server to your application: either make an RPC or set some global Javascript variables in the initial page load.

Assuming these are relatively simple constants that don't require heavy computation on the server, your best bet is probably to include them in the page load (that is in the HTML page that bootstraps your GWT application). For example, your page might look like:

<html>
<head>
...
<!-- This block would be generated by your server-side templating system -->
<script>
  var globalFoo = 1234; 
</script>
...
</head>
<body>
...

Then in your client, you can have a Gin module with a snippet of code like this:

@Provides
@Foo int providesFoo() {
  return getNativeFoo();
}

// Use JSNI to get the global Javascript variable.
private static native int getNativeFoo() /*-{
  return globalFoo;
}-*/;

This still requires you to have all your binding annotations hard-coded (even if you're using @Named annotations). In Gin, there's no way around this; all bindings need to be known at GWT compile time.

If you were to go with an RPC-based approach, then using Gin wouldn't provide much help. Instead you'd probably want to construct your objects before the RPC, listen on the RPC response, and then make updates based on the response.

Hope that answers your question.

Andrew McNamee
  • 1,705
  • 1
  • 13
  • 11
  • Well, I tried to follow the approach you described: created class A with public field B, then created method "@Provides @Singleton A getA()", withing the method getA I'm calling "public static native String initURI() /* .. */". And I have method: @ Provides @ Inject @ Singleton provideC(A a). Whilst it compiles well, it doesn't work because of some magic errors like "ClassNotFound" etc. I'm wondering what could I miss here? What is annotation @ Foo in your example? Should I create my own? Thanks! – jdevelop Nov 02 '12 at 19:03
  • The ClassNotFound error sounds odd. Can you give any more details? Is this a GWT runtime error? I'm not sure what might be causing that. The @Foo annotation would be a Guice binding annotation (http://code.google.com/p/google-guice/wiki/BindingAnnotations). This allows you to differentiate this integer from other ints that you might need to inject. As a rule of thumb, you'd want to name your annotation the same as what you'd name the variable if it were a method arg or local variable (e.g. "int foo = ..." -> "(@Foo int foo)") – Andrew McNamee Nov 06 '12 at 10:12