From: Manuel Carrasco MoƱino Date: Mon, 18 Mar 2013 10:42:35 +0000 (+0100) Subject: Promise helper for JsonpRequestBuilder X-Git-Tag: release-1.4.0~57^2~11 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=f164d87e71dee8b2a2fdfd398df1057033eb0a2d;p=gwtquery.git Promise helper for JsonpRequestBuilder --- 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 628ebb1c..c868398c 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 @@ -26,6 +26,7 @@ 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.jsonp.client.JsonpRequestBuilder; import com.google.gwt.query.client.Function; import com.google.gwt.query.client.GQuery; import com.google.gwt.query.client.Promise; @@ -116,7 +117,56 @@ public class Deferred extends GQuery implements Promise.Deferred { } } - + /** + * Utility class used to create promises for JsonpRequestBuilder. + *
+   *    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);
+   *      }
+   *    });
+   * 
+ */ + public static class PromiseJsonpReqBuilder extends DeferredPromiseImpl { + 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); + } + if (callbackParam != null) { + builder.setCallbackParam(callbackParam); + } + send(builder, url, new AsyncCallback() { + 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); + }-*/; + } + /** * Utility class used to create promises for RequestFactory services. *