From: Manuel Carrasco MoƱino Date: Mon, 18 Mar 2013 11:10:53 +0000 (+0100) Subject: Use RequestBuilder Deferred helpers in Ajax plugin to simply the code X-Git-Tag: release-1.4.0~57^2~9 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=a8d87b5aa088ce8d406ec1aa905f94186dce51c2;p=gwtquery.git Use RequestBuilder Deferred helpers in Ajax plugin to simply the code --- 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 86574b9a..323eb728 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 @@ -2,11 +2,8 @@ package com.google.gwt.query.client.plugins.ajax; import com.google.gwt.core.client.GWT; import com.google.gwt.dom.client.Element; -import com.google.gwt.http.client.Request; import com.google.gwt.http.client.RequestBuilder; import com.google.gwt.http.client.RequestBuilder.Method; -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; @@ -15,6 +12,8 @@ import com.google.gwt.query.client.Properties; import com.google.gwt.query.client.builders.JsonBuilder; import com.google.gwt.query.client.js.JsUtils; import com.google.gwt.query.client.plugins.Deferred; +import com.google.gwt.query.client.plugins.Deferred.PromiseJsonpReqBuilder; +import com.google.gwt.query.client.plugins.Deferred.PromiseReqBuilder; import com.google.gwt.query.client.plugins.Plugin; import com.google.gwt.user.client.ui.FormPanel; @@ -130,21 +129,11 @@ public class Ajax extends GQuery { return getJSONP(url, onSuccess, onError, timeout); } - final RequestBuilder requestBuilder = createRequestBuilder(settings, httpMethod, url, data); - requestBuilder.setCallback(new RequestCallback() { - public void onError(Request request, Throwable exception) { - dfd.reject(null, exception.getMessage(), request, null, exception); - } - - public void onResponseReceived(Request request, Response response) { - int statusCode = response.getStatusCode(); - if (statusCode <= 0 || statusCode >= 400) { - if (statusCode == 0) { - // Just warn the developer about the status code - GWT.log("GQuery.ajax error, the response.statusCode is 0, this usually happens when you try to access an external server without CORS enabled. url=" + url); - } - dfd.reject(response.getText(), "error", request, response); - } else if (onSuccess != null) { + createPromiseRequestBuilder(settings, httpMethod, url, data) + .done(new Function() { + public void f() { + Response response = arguments(0); + Response request = arguments(1); Object retData = null; try { if ("xml".equalsIgnoreCase(dataType)) { @@ -161,19 +150,19 @@ public class Ajax extends GQuery { } dfd.resolve(retData, "success", request, response); } - } - }); - - try { - requestBuilder.send(); - } catch (RequestException e) { - dfd.reject(null, e.getMessage(), null, null, e); - } - + }) + .fail(new Function() { + public void f() { + Throwable exception = arguments(0); + Response request = arguments(1); + dfd.reject(null, exception.getMessage(), request, null, exception); + } + }); + return dfd.promise(); } - private static RequestBuilder createRequestBuilder(Settings settings, Method httpMethod, String url, String data) { + private static Promise createPromiseRequestBuilder(Settings settings, Method httpMethod, String url, String data) { RequestBuilder requestBuilder = new RequestBuilder(httpMethod, url); @@ -210,7 +199,7 @@ public class Ajax extends GQuery { } } - return requestBuilder; + return new PromiseReqBuilder(requestBuilder); } private static String resolveUrl(Settings settings, Method httpMethod, String data) { @@ -327,18 +316,9 @@ public class Ajax extends GQuery { } public static Promise getJSONP(String url, Function success, Function error, int timeout) { - if (!url.contains("=?") && !url.contains("callback=")) { - url += (url.contains("?") ? "&" : "?") + "callback=?"; - } - url += "&_=" + System.currentTimeMillis(); - Element e = $("head").get(0); - if (e == null) { - e = document.getDocumentElement(); - } - Deferred dfd = $().as(Deferred.Deferred); - dfd.promise().done(success).fail(error == null ? success : error); - getJsonpImpl(e, url, null, dfd, timeout); - return dfd.promise(); + return new PromiseJsonpReqBuilder(url, null, timeout) + .done(success) + .fail(error == null ? success : error); } public static Promise post(String url, Properties data, final Function onSuccess) { @@ -392,40 +372,4 @@ public class Ajax extends GQuery { ajax(s); return this; } - - private static int callBackCounter = 0; - - private static native void getJsonpImpl(Element elem, String url, String charset, Deferred dfd, int timeout) /*-{ - var fName = "__GQ_cb_" + @com.google.gwt.query.client.plugins.ajax.Ajax::callBackCounter ++; - var done = false; - $wnd[fName] = function(data) { - if (!done) { - done = true; - $wnd[fName] = null; - dfd.@com.google.gwt.query.client.plugins.Deferred::ok(*)(data) - } - } - function err() { - if (!done) { - done = true; - $wnd[fName] = null; - dfd.@com.google.gwt.query.client.plugins.Deferred::err(*)(null) - } - } - if (timeout) { - setTimeout(err, timeout); - } - - url = url.replace(/=\?/g,'=' + fName); - var script = document.createElement("script"); - script.async = "async"; - if (charset) script.charset = charset; - script.src = url; - script.onload = script.onreadystatechange = function(evt) { - script.onload = script.onreadystatechange = null; - elem.removeChild(script); - err(); - }; - elem.insertBefore(script, elem.firstChild); - }-*/; }