I attached the class that we use. It's basically the approach Thomas
suggested and is most similar to the Dictionary class method mentioned in
the link he provided. Our class does some namespacing and handles any JSON
values like Objects, Arrays, ints, booleans, etc. and provides a defaulting
mechanism.
For initialization variables, we use the static get() and get(String)
methods. Soon after we created this class we added constructors to wrap any
JavaScriptObject or JSONObject because we found the whole isNumber,
isBoolean, etc API of JSONValue a bit cumbersome. Then we added the static
parse and parseStrict methods to avoid using the JSON APIs anywhere in our
code.
It is definitely best practice to provide these values in the host HTML
page as embedded Javascript.
If there's interest, I can include this in
our http://code.google.com/p/gwt-traction/ project so other people can just
use it as-is.
-Andy
On Monday, July 8, 2013 4:02:00 AM UTC-4, Thomas Broyer wrote:
>
>
>
> On Monday, July 8, 2013 3:58:29 AM UTC+2, Joel Malchiondo wrote:
>>
>> I would like to be able to pass a bunch of values from the server to the
>> client and store them on the client side inside global vars. I tried doing
>> this by creating a properties from a properties file on the server side and
>> returning this in an rpc call. but when i do this i get an error "No source
>> code is available for type java.util.Properties; did you forget to inherit
>> a required module?". I have tried googling around but i cant seem to find a
>> solution to this error. Does anyone have any ideas of how to fix the error
>> or how to pass the variables to the client a better way?
>
>
> IMO, a better way is to pass the values through the HTML host page:
> http://www.gwtproject.org/articles/dynamic_host_page.html
>
--
You received this message because you are subscribed to the Google Groups
"Google Web Toolkit" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/google-web-toolkit.
For more options, visit https://groups.google.com/groups/opt_out.
/****************************************************************
Traction Software, Inc. Confidential and Proprietary Information
Copyright (c) 1996-2013 Traction Software, Inc.
All rights reserved.
****************************************************************/
// PLEASE DO NOT DELETE THIS LINE -- make copyright depends on it.
package com.traction.gwt.traction.client;
import java.util.Set;
import com.google.gwt.core.client.GWT;
import com.google.gwt.core.client.JavaScriptObject;
import com.google.gwt.json.client.JSONArray;
import com.google.gwt.json.client.JSONBoolean;
import com.google.gwt.json.client.JSONNumber;
import com.google.gwt.json.client.JSONObject;
import com.google.gwt.json.client.JSONParser;
import com.google.gwt.json.client.JSONString;
import com.google.gwt.json.client.JSONValue;
/**
* Options are just a wrapper around a JSONObject with a standard
* layout.
*
* Currently we don't check for JSONNull which means that we can't
* specify null to override a default. I don't think we need to do
* that anyway, but I'll add it when needed. [ajm 16.Apr.2009]
*
* NOTE: Options.get() and Options.get(name) will never return null,
* even if there are no options specified in the view.
*
* --- Render in an HTML Script tag ---
*
* var gwtOptions = {};
*
* gwtOptions.Global = {
* proj: "foo"
* };
*
* gwtOptions.AddPrincipal = {
* isAclEditor: false
* };
*
*
* --- Access from GWT ---
*
* From anywhere:
* Options globalOptions = Options.get();
* Options options = Options.get(AddPrincipal.class);
*
* In AddPrincipal:
* Options options = Options.get(this);
*
*
*/
public final class Options {
// ----------------------------------------------------------------------
// static access to options
/**
* Returns the set of options for a specified object.
*/
public static final Options get(Object obj) {
return get(obj.getClass());
}
/**
* Returns the set of options for a specified object.
*/
public static final Options get(Class<?> cls) {
final String clsname = cls.getName();
final int period = clsname.lastIndexOf('.');
return get((period >= 0) ? clsname.substring(period+1) : clsname);
}
/**
* Returns the set of options with a specified name.
*/
public static final Options get(String name) {
init();
return root_.getOptions(name);
}
/**
* Returns the "global" options, where common properties like
* "proj" are specified.
*/
public static final Options get() {
init();
return global_;
}
// ----------------------------------------------------------------------
// static initializer
private static final native JavaScriptObject getRoot()
/*-{
return $wnd.gwtOptions || {};
}-*/;
private static Options root_;
private static Options global_;
private static final void init() {
if (root_ == null) {
root_ = new Options(new JSONObject(getRoot()));
global_ = root_.getOptions("Global");
}
}
// ----------------------------------------------------------------------
// initializer
private JSONObject options;
/**
* Creates new empty Options.
*/
public Options() {
this(new JSONObject());
}
/**
* Options wrapped around a {@link JavaScriptObject} typically
* returned from a native JSNI call.
*
* @param jso
* This will be wrapped in a JSONObject. If null, a new
* JSONObject() will be created so that getString(),
* etc will operate on an empty object.
*/
public Options(JavaScriptObject jso) {
this((jso != null) ? new JSONObject(jso) : null);
}
/**
* Options are just a wrapper around a {@link JSONObject}.
*
* @param options
* If null, a new JSONObject() will be created so that
* getString(), etc will operate on an empty object.
*/
public Options(JSONObject options) {
this.options = (options != null) ? options : new JSONObject();
}
/**
* This can be used to parse JSON from a response.
*/
public static final Options parseStrict(String json) {
return wrap(JSONParser.parseStrict(json));
}
/**
* Ideally parseStrict should be used for safety.
* @deprecated
*/
public static final Options parse(String json) {
return wrap(JSONParser.parse(json));
}
private static final Options wrap(JSONValue value) {
if (value != null) {
JSONObject obj = value.isObject();
if (obj != null) {
return new Options(obj);
}
else return null;
}
else return null;
}
// ----------------------------------------------------------------------
// read access
public final Set<String> keySet() {
return options.keySet();
}
public final boolean containsKey(String key) {
return options.containsKey(key);
}
/**
* Returns sub-options for this set of options
*/
public Options getOptions(String key) {
JSONValue val = options.get(key);
if (val != null && val.isObject() != null) {
return new Options(val.isObject());
} else {
// create a new JSONObject
JSONObject obj = new JSONObject();
options.put(key, obj);
return new Options(obj);
}
}
/**
* Returns an array of option elements.
*
* gwtOptions.Global = {
* projects: [ { name: "foo", displayName: "Foo" }, ... ];
* };
*/
public Options[] getOptionsArray(String key) {
JSONArray array = getArray(key);
if (array != null) {
int sz = array.size();
Options[] ret = new Options[sz];
for (int i=0; i<sz; i++) {
JSONObject obj = array.get(i).isObject();
ret[i] = new Options(obj);
}
return ret;
}
else {
return new Options[0];
}
}
public String getString(String key) {
return getString(key, null);
}
public String getString(String key, String def) {
return getString(options.get(key), def);
}
public double getNumber(String key) {
return getNumber(key, -1);
}
public double getNumber(String key, int def) {
return getNumber(options.get(key), def);
}
public boolean getBoolean(String key) {
return getBoolean(key, false);
}
public boolean getBoolean(String key, boolean def) {
return getBoolean(options.get(key), def);
}
/**
* Originally this was going to return String[], but it provides
* access to the raw JSONArray since this may have nested
* JSONValue that are more complex than a String.
*/
public JSONArray getArray(String key) {
return getArray(options.get(key));
}
/**
* Provides direct access to the data for complex configuration.
*/
public JSONObject getObject(String key) {
return getObject(options.get(key));
}
/**
* Provides direct access to the data for complex configuration.
*/
public JSONValue getValue(String key) {
return options.get(key);
}
// ----------------------------------------------------------------------
// write access
public void setString(String key, String val) {
options.put(key, new JSONString(val));
}
public void setNumber(String key, double val) {
options.put(key, new JSONNumber(val));
}
public void setBoolean(String key, boolean val) {
options.put(key, JSONBoolean.getInstance(val));
}
// ----------------------------------------------------------------------
// Like NativeTypeConversion for JSON
public static String getString(JSONValue val, String def) {
if (val == null) {
return def;
}
if (val.isString() != null) {
return val.isString().stringValue();
}
if (val.isNull() != null) {
return null;
}
GWT.log("Warning: trying to treat JSONValue " + val + " as String.");
return val.toString();
}
public static double getNumber(JSONValue val, int def) {
return (val != null && val.isNumber() != null) ? val.isNumber().doubleValue() : def;
}
public static boolean getBoolean(JSONValue val, boolean def) {
return (val != null && val.isBoolean() != null) ? val.isBoolean().booleanValue() : def;
}
public static JSONArray getArray(JSONValue val) {
return (val != null && val.isArray() != null) ? val.isArray() : null;
}
public static JSONObject getObject(JSONValue val) {
return (val != null && val.isObject() != null) ? val.isObject() : null;
}
/**
* Returns the JavaScriptObject for the specified Object. This can
* be passed to native JSNI methods.
*/
public JavaScriptObject getJavaScriptObject() {
return options.getJavaScriptObject();
}
}