]> source.dussan.org Git - gwtquery.git/commitdiff
Adding some utility classes to create Promises for GWT RequestBuilder, RPC and RFC...
authorManuel Carrasco Moñino <manuel.carrasco.m@gmail.com>
Sun, 17 Mar 2013 19:01:14 +0000 (20:01 +0100)
committerManuel Carrasco Moñino <manuel.carrasco.m@gmail.com>
Sun, 17 Mar 2013 19:01:14 +0000 (20:01 +0100)
gwtquery-core/src/main/java/com/google/gwt/query/client/Function.java
gwtquery-core/src/main/java/com/google/gwt/query/client/plugins/Deferred.java

index 8c170b8cbca0719851f63d9dd617d8b217c69953..83aae6adf2feb52cc297cf2e2169fe04c3b302bb 100644 (file)
@@ -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.
index fbef19d14c6e1aa588a36bb63a421d8aeb3d1c92..5c1ce1944017ab9f9dd22d6a8f6d572b4870ed7f 100644 (file)
@@ -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);