return $(Arrays.asList(widgets));
}
+ /**
+ * Create an empty JSON object.
+ */
+ public static Properties $$() {
+ return $$(null);
+ }
+
/**
* Wrap a JSON object.
*/
}
/**
- * We store data in js object which has this structure:
+ * Get the element data matching the key.
+ */
+ public static <T> T data(Element e, String key) {
+ return data(e, key, null);
+ }
+
+ /**
+ * Store arbitrary data associated with the specified element.
*
- * datacache [element_hash] [key] = value
+ * We store this data in a global js object having the structure:
+ * datacache [element.hashCode()] [key] = value
*
* @return the value stored in the element with the given name
*/
- protected static <S> Object data(Element element, String key, S value) {
- return data(element, key, value, Object.class);
+ public static <T> T data(Element element, String key, T value) {
+ return data(element, key, value, null);
}
- private static <S> Object data(Element element, String key, S value, Class<S> clz) {
+ private static <T> T data(Element element, String key, T value, Class<? extends T> clz) {
if (dataCache == null) {
windowData = JavaScriptObject.createObject().cast();
dataCache = JavaScriptObject.createObject().cast();
}
/**
- * Returns value at named data store for the element, as set by data(name, value).
+ * Return the value at the named data store for the first element in the set of matched
+ * elements.
*/
- public Object data(String name) {
- return isEmpty() ? null : data(get(0), name, null);
+ @SuppressWarnings("unchecked")
+ public <T> T data(String name) {
+ return isEmpty() ? null : (T)data(get(0), name, null);
}
/**
- * Returns value at named data store for the element, as set by data(name, value) with desired
- * return type.
+ * Return the value at the named data store for the first element in the set of matched
+ * elements, as set by data(name, value), with desired return type.
*
* @param clz return type class literal
*/
- @SuppressWarnings("unchecked")
- public <T> T data(String name, Class<T> clz) {
- return isEmpty() ? null : (T) data(get(0), name, null, clz);
+ public <T> T data(String name, Class<? extends T> clz) {
+ return isEmpty() ? null : data(get(0), name, null, clz);
}
/**
- * Stores the value in the named spot with desired return type.
+ * Store arbitrary data associated with the matched elements in the named data store.
*/
public GQuery data(String name, Object value) {
for (Element e : elements()) {
@SuppressWarnings("unchecked")
public final <T> T get(Object id, Class<? extends T> clz) {
Object o = get(id);
- if (o instanceof Double) {
- Double d = (Double)o;
- if (clz == Float.class) o = d.floatValue();
- else if (clz == Integer.class) o = d.intValue();
- else if (clz == Long.class) o = d.longValue();
- else if (clz == Short.class) o = d.shortValue();
- else if (clz == Byte.class) o = d.byteValue();
- } else if (clz == Boolean.class && !(o instanceof Boolean)) {
- o = Boolean.valueOf(String.valueOf(o));
- } else if (clz == String.class && !(o instanceof String)) {
- o = String.valueOf(o);
+ if (clz != null) {
+ if (o instanceof Double) {
+ Double d = (Double)o;
+ if (clz == Float.class) o = d.floatValue();
+ else if (clz == Integer.class) o = d.intValue();
+ else if (clz == Long.class) o = d.longValue();
+ else if (clz == Short.class) o = d.shortValue();
+ else if (clz == Byte.class) o = d.byteValue();
+ } else if (clz == Boolean.class && !(o instanceof Boolean)) {
+ o = Boolean.valueOf(String.valueOf(o));
+ } else if (clz == String.class && !(o instanceof String)) {
+ o = String.valueOf(o);
+ }
}
return (T)o;
}
assertEquals("red", $(parent).css(CSS.BACKGROUND_COLOR, false));
}
+ public void testData() {
+
+ assertEquals(null, $().data("whatever"));
+
+ Object o = $$();
+
+ GQuery g = $(e)
+ .data("number", 3.5)
+ .data("bool", true)
+ .data("string", "foo")
+ .data("object", o);
+
+ double d = g.data("number");
+ assertEquals(3.5d, d);
+ int i = g.data("number", Integer.class);
+ assertEquals(3, i);
+ long l = g.data("number", Long.class);
+ assertEquals(3l, l);
+
+ boolean b = g.data("bool");
+ assertTrue(b);
+
+ String s = g.data("string");
+ assertEquals("foo", s);
+ s = g.data("bool", String.class);
+ assertEquals("true", s);
+
+ Element n = GQuery.data(e, "object");
+ assertEquals(o, n);
+ }
+
public void testDetachMethod(){
String html = "<div id='parent'>parent<div id='child'>child</div></div>";
$(e).html(html);