diff options
author | Manuel Carrasco Moñino <manuel.carrasco.m@gmail.com> | 2013-12-28 23:26:57 +0100 |
---|---|---|
committer | Manuel Carrasco Moñino <manuel.carrasco.m@gmail.com> | 2013-12-28 23:26:57 +0100 |
commit | 1e3810ad25a55a3f17be29e2627f89ec51bab07c (patch) | |
tree | 67f5809849000f7134c31993692314cb1367d97f | |
parent | 954b0e839a6ee93da7aece0822f558f7124d0942 (diff) | |
download | gwtquery-1e3810ad25a55a3f17be29e2627f89ec51bab07c.tar.gz gwtquery-1e3810ad25a55a3f17be29e2627f89ec51bab07c.zip |
Using Binder interface in certain ajax properties
6 files changed, 108 insertions, 21 deletions
diff --git a/gwtquery-core/src/main/java/com/google/gwt/query/client/Binder.java b/gwtquery-core/src/main/java/com/google/gwt/query/client/Binder.java new file mode 100644 index 00000000..c43122ac --- /dev/null +++ b/gwtquery-core/src/main/java/com/google/gwt/query/client/Binder.java @@ -0,0 +1,68 @@ +/* + * Copyright 2013, The gwtquery team. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not + * use this file except in compliance with the License. You may obtain a copy of + * the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations under + * the License. + */ +package com.google.gwt.query.client; + + +/** + * Interface using for Data Binders valid for JVM and JS. + */ +public interface Binder { + /** + * load a properties object. + */ + <J> J load(Object prp); + + /** + * parses a json string and loads the resulting properties object. + */ + <J> J parse(String json); + + /** + * Returns the underlying object, normally a Properties jso in client + * and a Json implementation in JVM. + */ + <J> J getBound(); + + /** + * Return the Object with the given key. + */ + <T> T get(Object key); + + /** + * Set an Object with the given key. + */ + <T> T set(Object key, Object val); + + /** + * return a list of field names. + */ + String[] getFieldNames(); + + /** + * return a string which represents the object with an alias for the className + */ + String toJson(); + + /** + * return a string which represents the object in a queryString format. + */ + String toQueryString(); + + /** + * return the name for this type + */ + String getName(); +} diff --git a/gwtquery-core/src/main/java/com/google/gwt/query/client/Properties.java b/gwtquery-core/src/main/java/com/google/gwt/query/client/Properties.java index af671474..b8e341c4 100644 --- a/gwtquery-core/src/main/java/com/google/gwt/query/client/Properties.java +++ b/gwtquery-core/src/main/java/com/google/gwt/query/client/Properties.java @@ -179,8 +179,10 @@ public class Properties extends JavaScriptObject implements Binder { this[name].__f = f; }-*/; - public final <T, O> void set(T name, O val) { + @SuppressWarnings("unchecked") + public final Properties set(Object name, Object val) { c().put(String.valueOf(name), val); + return this; } public final String tostring() { diff --git a/gwtquery-core/src/main/java/com/google/gwt/query/client/builders/JsonBuilderBase.java b/gwtquery-core/src/main/java/com/google/gwt/query/client/builders/JsonBuilderBase.java index af3a5128..61cca6f3 100644 --- a/gwtquery-core/src/main/java/com/google/gwt/query/client/builders/JsonBuilderBase.java +++ b/gwtquery-core/src/main/java/com/google/gwt/query/client/builders/JsonBuilderBase.java @@ -116,4 +116,10 @@ public abstract class JsonBuilderBase<J extends JsonBuilderBase<?>> implements J public <T> T get(Object key) { return p.get(key); } + + @SuppressWarnings("unchecked") + public <T> T set(Object key, Object val) { + p.set(key, val); + return (T)this; + } } diff --git a/gwtquery-core/src/main/java/com/google/gwt/query/client/plugins/ajax/Ajax.java b/gwtquery-core/src/main/java/com/google/gwt/query/client/plugins/ajax/Ajax.java index 3170c3cb..9e99f3b5 100644 --- a/gwtquery-core/src/main/java/com/google/gwt/query/client/plugins/ajax/Ajax.java +++ b/gwtquery-core/src/main/java/com/google/gwt/query/client/plugins/ajax/Ajax.java @@ -4,12 +4,15 @@ import com.google.gwt.core.client.Callback; import com.google.gwt.core.client.GWT; import com.google.gwt.core.client.ScriptInjector; import com.google.gwt.dom.client.Element; +import com.google.gwt.dom.client.ScriptElement; import com.google.gwt.http.client.Request; import com.google.gwt.http.client.Response; import com.google.gwt.query.client.Function; +import com.google.gwt.query.client.GQ; import com.google.gwt.query.client.GQuery; import com.google.gwt.query.client.Promise; import com.google.gwt.query.client.Properties; +import com.google.gwt.query.client.Binder; import com.google.gwt.query.client.builders.JsonBuilder; import com.google.gwt.query.client.js.JsUtils; import com.google.gwt.query.client.plugins.Plugin; @@ -39,11 +42,11 @@ public class Ajax extends GQuery { public interface Settings extends JsonBuilder { String getContentType(); Element getContext(); - Properties getData(); + Binder getData(); String getDataString(); String getDataType(); Function getError(); - Properties getHeaders(); + Binder getHeaders(); String getPassword(); Function getSuccess(); int getTimeout(); @@ -52,11 +55,11 @@ public class Ajax extends GQuery { String getUsername(); Settings setContentType(String t); Settings setContext(Element e); - Settings setData(Properties p); + Settings setData(Object p); Settings setDataString(String d); Settings setDataType(String t); Settings setError(Function f); - Settings setHeaders(Properties p); + Settings setHeaders(Binder p); Settings setPassword(String p); Settings setSuccess(Function f); Settings setTimeout(int t); @@ -172,16 +175,21 @@ public class Ajax extends GQuery { private static Promise createPromiseScriptInjector(final String url) { return new PromiseFunction() { + private ScriptElement scriptElement; public void f(final Deferred dfd) { - ScriptInjector.fromUrl(url).setWindow(window) + scriptElement = ScriptInjector.fromUrl(url).setWindow(window) .setCallback(new Callback<Void, Exception>() { public void onSuccess(Void result) { - dfd.resolve(); + $(window).delay(0, new Function(){ + public void f() { + dfd.resolve(scriptElement); + } + }); } public void onFailure(Exception reason) { dfd.reject(reason); } - }).inject(); + }).inject().cast(); } }; } @@ -197,15 +205,15 @@ public class Ajax extends GQuery { private static Object resolveData(Settings settings, String httpMethod) { Object data = settings.getDataString(); - Properties sdata = settings.getData(); + Binder sdata = settings.getData(); if (data == null && sdata != null) { String type = settings.getDataType(); if (type != null && (httpMethod.matches("(POST|PUT)")) && type.equalsIgnoreCase("json")) { - data = sdata.toJsonString(); - } else if (JsUtils.isFormData(sdata)) { - data = sdata; + data = sdata.toString(); +// } else if (JsUtils.isFormData(sdata)) { +// data = sdata; } else { data = sdata.toQueryString(); } @@ -244,9 +252,9 @@ public class Ajax extends GQuery { return createSettings($$(prop)); } - public static Settings createSettings(Properties p) { - Settings s = GWT.create(Settings.class); - s.load(p); + public static Settings createSettings(Binder p) { + Settings s = GQ.create(Settings.class); + s.load(p.getBound()); return s; } diff --git a/gwtquery-core/src/main/java/com/google/gwt/query/client/plugins/deferred/PromiseReqBuilder.java b/gwtquery-core/src/main/java/com/google/gwt/query/client/plugins/deferred/PromiseReqBuilder.java index 61273296..3d1d0194 100644 --- a/gwtquery-core/src/main/java/com/google/gwt/query/client/plugins/deferred/PromiseReqBuilder.java +++ b/gwtquery-core/src/main/java/com/google/gwt/query/client/plugins/deferred/PromiseReqBuilder.java @@ -21,8 +21,8 @@ import com.google.gwt.http.client.RequestCallback; import com.google.gwt.http.client.RequestException; import com.google.gwt.http.client.RequestPermissionException; import com.google.gwt.http.client.Response; +import com.google.gwt.query.client.Binder; import com.google.gwt.query.client.Function; -import com.google.gwt.query.client.Properties; import com.google.gwt.query.client.js.JsCache; import com.google.gwt.query.client.js.JsUtils; import com.google.gwt.query.client.plugins.ajax.Ajax.Settings; @@ -102,10 +102,10 @@ public class PromiseReqBuilder extends DeferredPromiseImpl implements RequestCal } })); - Properties headers = settings.getHeaders(); + Binder headers = settings.getHeaders(); if (headers != null) { - for (String headerKey : headers.keys()) { - xmlHttpRequest.setRequestHeader(headerKey, headers.getStr(headerKey)); + for (String headerKey : headers.getFieldNames()) { + xmlHttpRequest.setRequestHeader(headerKey, String.valueOf(headers.get(headerKey))); } } diff --git a/gwtquery-core/src/main/java/com/google/gwt/query/rebind/JsonBuilderGenerator.java b/gwtquery-core/src/main/java/com/google/gwt/query/rebind/JsonBuilderGenerator.java index 8c9c7408..2e66bc3f 100644 --- a/gwtquery-core/src/main/java/com/google/gwt/query/rebind/JsonBuilderGenerator.java +++ b/gwtquery-core/src/main/java/com/google/gwt/query/rebind/JsonBuilderGenerator.java @@ -184,6 +184,9 @@ public class JsonBuilderGenerator extends Generator { String q = method.getReturnType().getQualifiedSourceName(); sw.println("return " + "((" + q + ")GWT.create(" + q + ".class))" + ".load(p.getJavaScriptObject(\"" + name + "\"));"); + } else if (isTypeAssignableTo(method.getReturnType(), settingsType)) { + String q = method.getReturnType().getQualifiedSourceName(); + sw.println("return " + "((" + q + ")p.getJavaScriptObject(\"" + name + "\"));"); } else if (retType.equals(Properties.class.getName())) { sw.println("return getPropertiesBase(\"" + name + "\");"); } else if (isTypeAssignableTo(method.getReturnType(), jsType)) { @@ -218,8 +221,8 @@ public class JsonBuilderGenerator extends Generator { sw.println("return Arrays.asList(" + ret + ");"); } } else if (method.getReturnType().isEnum() != null){ - sw.println("return "+method.getReturnType().getQualifiedSourceName()+".valueOf(p.getStr(\"" + name + "\"));"); - }else { + sw.println("return "+ method.getReturnType().getQualifiedSourceName() + ".valueOf(p.getStr(\"" + name + "\"));"); + } else { sw.println("System.err.println(\"JsonBuilderGenerator WARN: unknown return type " + retType + " " + ifaceName + "." + name + "()\"); "); // We return the object because probably the user knows how to handle it |