aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorManuel Carrasco Moñino <manuel.carrasco.m@gmail.com>2013-03-17 20:01:14 +0100
committerManuel Carrasco Moñino <manuel.carrasco.m@gmail.com>2013-03-17 20:01:14 +0100
commit0ecb7191936866095c7d79cbafaec3d51a2b8af0 (patch)
tree8c883af77762fb473feacd0de96bdf362698b967
parent464f849ed1a41645ea8db6084ffdb8948ccba1c1 (diff)
downloadgwtquery-0ecb7191936866095c7d79cbafaec3d51a2b8af0.tar.gz
gwtquery-0ecb7191936866095c7d79cbafaec3d51a2b8af0.zip
Adding some utility classes to create Promises for GWT RequestBuilder, RPC and RFC services
-rw-r--r--gwtquery-core/src/main/java/com/google/gwt/query/client/Function.java15
-rw-r--r--gwtquery-core/src/main/java/com/google/gwt/query/client/plugins/Deferred.java108
2 files changed, 117 insertions, 6 deletions
diff --git a/gwtquery-core/src/main/java/com/google/gwt/query/client/Function.java b/gwtquery-core/src/main/java/com/google/gwt/query/client/Function.java
index 8c170b8c..83aae6ad 100644
--- a/gwtquery-core/src/main/java/com/google/gwt/query/client/Function.java
+++ b/gwtquery-core/src/main/java/com/google/gwt/query/client/Function.java
@@ -138,6 +138,21 @@ public abstract class Function {
public <T> T getArgument(int idx) {
return getArgument(-1, idx, null);
}
+
+ /**
+ * Convenience alias for the getArguments(idx) method thought just to
+ * make gQuery code look closed to jQuery.
+ */
+ public <T> T arguments(int idx) {
+ return getArgument(idx);
+ }
+
+ /**
+ * Convenience alias for the getArguments(argIdx, pos) method;
+ */
+ public <T> T arguments(int argIdx, int pos) {
+ return getArgument(argIdx, pos);
+ }
/**
* Safety return the argument in the position idx.
diff --git a/gwtquery-core/src/main/java/com/google/gwt/query/client/plugins/Deferred.java b/gwtquery-core/src/main/java/com/google/gwt/query/client/plugins/Deferred.java
index fbef19d1..5c1ce194 100644
--- a/gwtquery-core/src/main/java/com/google/gwt/query/client/plugins/Deferred.java
+++ b/gwtquery-core/src/main/java/com/google/gwt/query/client/plugins/Deferred.java
@@ -17,11 +17,22 @@ import static com.google.gwt.query.client.Promise.PENDING;
import static com.google.gwt.query.client.Promise.REJECTED;
import static com.google.gwt.query.client.Promise.RESOLVED;
+import java.util.Set;
+
+import javax.validation.ConstraintViolation;
+
+import com.google.gwt.http.client.Request;
+import com.google.gwt.http.client.RequestBuilder;
+import com.google.gwt.http.client.RequestCallback;
+import com.google.gwt.http.client.RequestException;
+import com.google.gwt.http.client.Response;
import com.google.gwt.query.client.Function;
import com.google.gwt.query.client.GQuery;
import com.google.gwt.query.client.Promise;
import com.google.gwt.query.client.plugins.callbacks.Callbacks;
import com.google.gwt.user.client.rpc.AsyncCallback;
+import com.google.web.bindery.requestfactory.shared.Receiver;
+import com.google.web.bindery.requestfactory.shared.ServerFailure;
/**
* Implementation of jQuery.Deferred for gwtquery.
@@ -64,10 +75,94 @@ public class Deferred extends GQuery implements Promise.Deferred {
}
/**
- * Utility class used to create promises for RPC CallBacks.
+ * Utility class used to create promises for RequestBuilder.
+ * <pre>
+ * RequestBuilder builder = new RequestBuilder(RequestBuilder.GET, "http://127.0.0.1:8888/whatever");
+ * PromiseRequest gettingResponse = new PromiseRequest(builder);
+ *
+ * gettingResponse.fail(new Function() {
+ * public void f() {
+ * Throwable exception = arguments(0);
+ * }
+ * }).done(new Function() {
+ * public void f() {
+ * Response response = arguments(0);
+ * }
+ * });
+ * </pre>
+ */
+ public static class PromiseReqBuilder extends DeferredPromiseImpl implements RequestCallback {
+ public PromiseReqBuilder(RequestBuilder builder) {
+ builder.setCallback(this);
+ try {
+ builder.send();
+ } catch (RequestException e) {
+ onError(null, e);
+ }
+ }
+
+ public void onError(Request request, Throwable exception) {
+ dfd.reject(exception, request);
+ }
+
+ public void onResponseReceived(Request request, Response response) {
+ int status = response.getStatusCode();
+ if (status <= 0 || status >= 400) {
+ String statusText = status <= 0 ? "Bad CORS" : response.getStatusText();
+ onError(request, new RequestException("HTTP ERROR: " + status + " " + statusText + "\n" + response.getText()));
+ } else {
+ dfd.resolve(response, request);
+ }
+ }
+ }
+
+
+ /**
+ * Utility class used to create promises for RequestFactory services.
+ * <pre>
+ * Request<SessionProxy> req1 = loginFact.api().login(null, null);
+ * Request<UserProxy> req2 = srvFact.api().getCurrentUser();
+ *
+ * Deferred.when(new PromiseRF(req1), new PromiseRF(req2)
+ * .done(new Function() {
+ * public void f() {
+ * SessionProxy session = arguments(0, 0);
+ * UserProxy user = arguments(1, 0);
+ * }
+ * })
+ * .fail(new Function() {
+ * public void f() {
+ * ServerFailure failure = arguments(0);
+ * }
+ * });
+ * </pre>
+ */
+ public static class PromiseRF extends DeferredPromiseImpl {
+ public <T> PromiseRF(com.google.web.bindery.requestfactory.shared.Request<T> request) {
+ request.fire(new Receiver<T>() {
+ public void onConstraintViolation(Set<ConstraintViolation<?>> violations) {
+ dfd.reject(new ServerFailure("ConstraintViolation"), violations);
+ }
+
+ public void onFailure(ServerFailure error) {
+ dfd.reject(error);
+ }
+
+ public void onSuccess(T response) {
+ dfd.resolve(response);
+ }
+ });
+ }
+ }
+
+ /**
+ * Utility class used to create promises for RPC services.
* <pre>
* PromiseRPC<String> gretting = new PromiseRPC<String>();
*
+ * GreetingServiceAsync greetingService = GWT.create(GreetingService.class);
+ * greetingService.greetServer("hi", gretting);
+ *
* gretting.fail(new Function(){
* public void f() {
* Throwable error = getArgument(0);
@@ -88,20 +183,21 @@ public class Deferred extends GQuery implements Promise.Deferred {
dfd.resolve(result);
}
}
+
/**
* Implementation of the Promise interface which is used internally by Deferred.
*/
private static class DeferredPromiseImpl implements Promise {
- protected com.google.gwt.query.client.plugins.Deferred dfd;
+ com.google.gwt.query.client.plugins.Deferred dfd;
- protected DeferredPromiseImpl(com.google.gwt.query.client.plugins.Deferred o) {
- dfd = o;
- }
-
protected DeferredPromiseImpl() {
dfd = new com.google.gwt.query.client.plugins.Deferred();
}
+
+ protected DeferredPromiseImpl(com.google.gwt.query.client.plugins.Deferred o) {
+ dfd = o;
+ }
public Promise always(Function... f) {
return done(f).fail(f);