Domanda

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?

È stato utile?

Soluzione

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.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top