]> source.dussan.org Git - gwtquery.git/commitdiff
Add two methods present in the jquery Promise object
authorManuel Carrasco Moñino <manuel.carrasco.m@gmail.com>
Mon, 18 Mar 2013 12:06:08 +0000 (13:06 +0100)
committerManuel Carrasco Moñino <manuel.carrasco.m@gmail.com>
Mon, 18 Mar 2013 12:06:08 +0000 (13:06 +0100)
gwtquery-core/src/main/java/com/google/gwt/query/client/Promise.java
gwtquery-core/src/main/java/com/google/gwt/query/client/plugins/ajax/Ajax.java
gwtquery-core/src/main/java/com/google/gwt/query/client/plugins/deferred/Deferred.java
gwtquery-core/src/main/java/com/google/gwt/query/client/plugins/deferred/PromiseJsonpReqBuilder.java [deleted file]
gwtquery-core/src/main/java/com/google/gwt/query/client/plugins/deferred/PromiseReqBuilderJSONP.java [new file with mode: 0644]

index 7d14b4390cccb0afcabcb60e5dbac4b845006a82..03c7327bf3f0dda9730918e1c0ea456944b77ed4 100644 (file)
@@ -95,4 +95,14 @@ public interface Promise {
    *   3rd one will be called when progress notifications are sent.  
    */
   Promise then(Function... f);
+  
+  /**
+   * Determine whether a Deferred object has been resolved.
+   */
+  boolean isResolved();
+  
+  /**
+   * Determine whether a Deferred object has been rejected.
+   */
+  boolean isRejected();
 }
index 8d2ed1dcc8c2a9382eee7a8388eb10fe63d5c651..fefcb67f79a036e58d4709f564c4e68f2778e458 100644 (file)
@@ -13,7 +13,7 @@ import com.google.gwt.query.client.builders.JsonBuilder;
 import com.google.gwt.query.client.js.JsUtils;
 import com.google.gwt.query.client.plugins.Plugin;
 import com.google.gwt.query.client.plugins.deferred.Deferred;
-import com.google.gwt.query.client.plugins.deferred.PromiseJsonpReqBuilder;
+import com.google.gwt.query.client.plugins.deferred.PromiseReqBuilderJSONP;
 import com.google.gwt.query.client.plugins.deferred.PromiseReqBuilder;
 import com.google.gwt.user.client.ui.FormPanel;
 
@@ -316,7 +316,7 @@ public class Ajax extends GQuery {
   }
 
   public static Promise getJSONP(String url, Function success, Function error, int timeout) {
-    return new PromiseJsonpReqBuilder(url, null, timeout)
+    return new PromiseReqBuilderJSONP(url, null, timeout)
        .done(success)
        .fail(error);
   }
index b2ab8d14bac03bc72b87f6d7d5456d071c294f0c..5b26df5dc10a6c4732862b5ef2422164fa15feda 100644 (file)
@@ -77,6 +77,14 @@ public class Deferred extends GQuery implements Promise.Deferred {
       }
       return this;
     }
+
+    public boolean isResolved() {
+      return Promise.RESOLVED.equals(state());
+    }
+
+    public boolean isRejected() {
+      return Promise.REJECTED.equals(state());
+    }
   }
   
   /**
diff --git a/gwtquery-core/src/main/java/com/google/gwt/query/client/plugins/deferred/PromiseJsonpReqBuilder.java b/gwtquery-core/src/main/java/com/google/gwt/query/client/plugins/deferred/PromiseJsonpReqBuilder.java
deleted file mode 100644 (file)
index 80f0082..0000000
+++ /dev/null
@@ -1,68 +0,0 @@
-package com.google.gwt.query.client.plugins.deferred;
-
-import com.google.gwt.jsonp.client.JsonpRequestBuilder;
-import com.google.gwt.query.client.js.JsObjectArray;
-import com.google.gwt.query.client.js.JsRegexp;
-import com.google.gwt.query.client.plugins.deferred.Deferred.DeferredPromiseImpl;
-import com.google.gwt.user.client.rpc.AsyncCallback;
-
-/**
- * Utility class used to create promises for JsonpRequestBuilder.
- * <pre>
- *    Promise p = new PromiseJsonpReqBuilder(url, 4000);
- *    
- *    p.done(new Function() {
- *      public void f() {
- *        Properties p = arguments(0);
- *      }
- *    }).fail(new Function() {
- *      public void f() {
- *        Throwable error = arguments(0);
- *      }
- *    });
- * </pre>
- */
-public class PromiseJsonpReqBuilder extends DeferredPromiseImpl {
-  
-  private static final JsRegexp callbackRegex = new JsRegexp("^(.+[\\?&])([^=]+)=\\?(.*)$");
-  
-  public PromiseJsonpReqBuilder(String url) {
-    this(url, null, 0);
-  }
-
-  public PromiseJsonpReqBuilder(String url, int timeout) {
-    this(url, null, timeout);
-  }
-
-  public PromiseJsonpReqBuilder(String url, String callbackParam, int timeout) {
-    JsonpRequestBuilder builder = new JsonpRequestBuilder();
-    if (timeout > 0) {
-      builder.setTimeout(timeout);
-    }
-    // jQuery allows a parameter callback=? to figure out the callback parameter
-    if (callbackParam == null) {
-      JsObjectArray<String> tmp = callbackRegex.exec(url);
-      if  (tmp.length() == 4) {
-        callbackParam = tmp.get(2);
-        url = tmp.get(1) + tmp.get(3);
-      }
-    }
-    if (callbackParam != null) {
-      builder.setCallbackParam(callbackParam);
-    }
-    send(builder, url, new AsyncCallback<Object>() {
-      public void onFailure(Throwable caught) {
-        dfd.reject(caught);
-      }
-
-      public void onSuccess(Object result) {
-        dfd.resolve(result);
-      }
-    });
-  }
-
-  // Using jsni because method send in JsonpRequestBuilder is private
-  private final native void send(JsonpRequestBuilder bld, String url, AsyncCallback<?> cb) /*-{
-    bld.@com.google.gwt.jsonp.client.JsonpRequestBuilder::send(Ljava/lang/String;Lcom/google/gwt/user/client/rpc/AsyncCallback;Z)(url,cb,false);
-  }-*/;
-}
\ No newline at end of file
diff --git a/gwtquery-core/src/main/java/com/google/gwt/query/client/plugins/deferred/PromiseReqBuilderJSONP.java b/gwtquery-core/src/main/java/com/google/gwt/query/client/plugins/deferred/PromiseReqBuilderJSONP.java
new file mode 100644 (file)
index 0000000..29a3d06
--- /dev/null
@@ -0,0 +1,68 @@
+package com.google.gwt.query.client.plugins.deferred;
+
+import com.google.gwt.jsonp.client.JsonpRequestBuilder;
+import com.google.gwt.query.client.js.JsObjectArray;
+import com.google.gwt.query.client.js.JsRegexp;
+import com.google.gwt.query.client.plugins.deferred.Deferred.DeferredPromiseImpl;
+import com.google.gwt.user.client.rpc.AsyncCallback;
+
+/**
+ * Utility class used to create promises for JsonpRequestBuilder.
+ * <pre>
+ *    Promise p = new PromiseJsonpReqBuilder(url, 4000);
+ *    
+ *    p.done(new Function() {
+ *      public void f() {
+ *        Properties p = arguments(0);
+ *      }
+ *    }).fail(new Function() {
+ *      public void f() {
+ *        Throwable error = arguments(0);
+ *      }
+ *    });
+ * </pre>
+ */
+public class PromiseReqBuilderJSONP extends DeferredPromiseImpl {
+  
+  private static final JsRegexp callbackRegex = new JsRegexp("^(.+[\\?&])([^=]+)=\\?(.*)$");
+  
+  public PromiseReqBuilderJSONP(String url) {
+    this(url, null, 0);
+  }
+
+  public PromiseReqBuilderJSONP(String url, int timeout) {
+    this(url, null, timeout);
+  }
+
+  public PromiseReqBuilderJSONP(String url, String callbackParam, int timeout) {
+    JsonpRequestBuilder builder = new JsonpRequestBuilder();
+    if (timeout > 0) {
+      builder.setTimeout(timeout);
+    }
+    // jQuery allows a parameter callback=? to figure out the callback parameter
+    if (callbackParam == null) {
+      JsObjectArray<String> tmp = callbackRegex.exec(url);
+      if  (tmp.length() == 4) {
+        callbackParam = tmp.get(2);
+        url = tmp.get(1) + tmp.get(3);
+      }
+    }
+    if (callbackParam != null) {
+      builder.setCallbackParam(callbackParam);
+    }
+    send(builder, url, new AsyncCallback<Object>() {
+      public void onFailure(Throwable caught) {
+        dfd.reject(caught);
+      }
+
+      public void onSuccess(Object result) {
+        dfd.resolve(result);
+      }
+    });
+  }
+
+  // Using jsni because method send in JsonpRequestBuilder is private
+  private final native void send(JsonpRequestBuilder bld, String url, AsyncCallback<?> cb) /*-{
+    bld.@com.google.gwt.jsonp.client.JsonpRequestBuilder::send(Ljava/lang/String;Lcom/google/gwt/user/client/rpc/AsyncCallback;Z)(url,cb,false);
+  }-*/;
+}
\ No newline at end of file