From e68e750c83706edd12e384806cfca4b63aab2443 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Manuel=20Carrasco=20Mo=C3=B1ino?= Date: Mon, 7 Oct 2013 13:28:26 +0200 Subject: [PATCH] Implementation of jquery getScript method in Ajax plugin --- .../gwt/query/client/plugins/ajax/Ajax.java | 86 +++++++++++++++---- .../gwt/query/client/GQueryAjaxTestGwt.java | 30 +++++++ 2 files changed, 97 insertions(+), 19 deletions(-) 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 8bbd2def..d50d7470 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 @@ -1,6 +1,8 @@ package com.google.gwt.query.client.plugins.ajax; +import com.google.gwt.core.client.Callback; import com.google.gwt.core.client.GWT; +import com.google.gwt.core.client.ScriptInjector; import com.google.gwt.dom.client.Element; import com.google.gwt.http.client.Request; import com.google.gwt.http.client.RequestBuilder; @@ -13,6 +15,7 @@ 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.Plugin; +import com.google.gwt.query.client.plugins.deferred.PromiseFunction; import com.google.gwt.query.client.plugins.deferred.PromiseReqBuilder; import com.google.gwt.query.client.plugins.deferred.PromiseReqBuilderJSONP; import com.google.gwt.user.client.ui.FormPanel; @@ -121,10 +124,14 @@ public class Ajax extends GQuery { final String url = resolveUrl(settings, httpMethod, data); final String dataType = settings.getDataType(); + Promise ret = null; + if ("jsonp".equalsIgnoreCase(dataType)) { - return getJSONP(url, onSuccess, onError, settings.getTimeout()); + ret = new PromiseReqBuilderJSONP(url, null, settings.getTimeout()); + } else if ("script".equalsIgnoreCase(dataType)){ + ret = createPromiseScriptInjector(url); } else { - return createPromiseRequestBuilder(settings, httpMethod, url, data) + ret = createPromiseRequestBuilder(settings, httpMethod, url, data) .then(new Function() { public Object f(Object...args) { Response response = (Response)args[0]; @@ -151,10 +158,15 @@ public class Ajax extends GQuery { Request request = (Request)args[1]; return new Object[]{null, exception.getMessage(), request, null, exception}; } - }) - .done(onSuccess) - .fail(onError); + }); + } + if (onSuccess != null) { + ret.done(onSuccess); + } + if (onError != null) { + ret.fail(onError); } + return ret; } private static Promise createPromiseRequestBuilder(Settings settings, Method httpMethod, String url, String data) { @@ -197,6 +209,21 @@ public class Ajax extends GQuery { return new PromiseReqBuilder(requestBuilder); } + private static Promise createPromiseScriptInjector(final String url) { + return new PromiseFunction() { + public void f(final Deferred dfd) { + ScriptInjector.fromUrl(url).setCallback(new Callback() { + public void onSuccess(Void result) { + dfd.resolve(); + } + public void onFailure(Exception reason) { + dfd.reject(reason); + } + }).inject(); + } + }; + } + private static String resolveUrl(Settings settings, Method httpMethod, String data) { String url = settings.getUrl(); assert url != null : "no url found in settings"; @@ -225,17 +252,17 @@ public class Ajax extends GQuery { String method = settings.getType(); if ("get".equalsIgnoreCase(method)) { return RequestBuilder.GET; - }else if("post".equalsIgnoreCase(method)){ - return RequestBuilder.POST; - }else if("put".equalsIgnoreCase(method)){ - return RequestBuilder.PUT; - }else if("delete".equalsIgnoreCase(method)){ - return RequestBuilder.DELETE; - }else if("head".equalsIgnoreCase(method)){ - return RequestBuilder.HEAD; + } else if ("post".equalsIgnoreCase(method)) { + return RequestBuilder.POST; + } else if ("put".equalsIgnoreCase(method)) { + return RequestBuilder.PUT; + } else if ("delete".equalsIgnoreCase(method)) { + return RequestBuilder.DELETE; + } else if ("head".equalsIgnoreCase(method)) { + return RequestBuilder.HEAD; } - GWT.log("unknow method type -> use POST as default method"); + GWT.log("GQuery.Ajax: no method type provided, using POST as default method"); return RequestBuilder.POST; } @@ -297,9 +324,9 @@ public class Ajax extends GQuery { } public static Promise getJSONP(String url) { - return getJSONP(url, null, null, 0); + return getJSONP(url, null, null); } - + public static Promise getJSONP(String url, Properties data, Function onSuccess) { Settings s = createSettings(); s.setUrl(url); @@ -311,9 +338,30 @@ public class Ajax extends GQuery { } public static Promise getJSONP(String url, Function success, Function error, int timeout) { - return new PromiseReqBuilderJSONP(url, null, timeout) - .done(success) - .fail(error); + return ajax(createSettings() + .setUrl(url) + .setDataType("jsonp") + .setType("get") + .setTimeout(timeout) + .setSuccess(success) + .setError(error) + ); + } + + /** + * Load a JavaScript file from the server using a GET HTTP request, then execute it. + */ + public static Promise getScript(String url) { + return getScript(url, null); + } + + public static Promise getScript(final String url, Function success) { + return ajax(createSettings() + .setUrl(url) + .setType("get") + .setDataType("script") + .setSuccess(success) + ); } public static Promise post(String url, Properties data, final Function onSuccess) { diff --git a/gwtquery-core/src/test/java/com/google/gwt/query/client/GQueryAjaxTestGwt.java b/gwtquery-core/src/test/java/com/google/gwt/query/client/GQueryAjaxTestGwt.java index 16e2b7d4..0c86c8fc 100644 --- a/gwtquery-core/src/test/java/com/google/gwt/query/client/GQueryAjaxTestGwt.java +++ b/gwtquery-core/src/test/java/com/google/gwt/query/client/GQueryAjaxTestGwt.java @@ -309,4 +309,34 @@ public class GQueryAjaxTestGwt extends GWTTestCase { }); } + public void testGetScript() { + delayTestFinish(5000); + String url = "http://code.jquery.com/jquery-2.0.3.min.js"; + Ajax.getScript(url) + .done(new Function(){ + public void f() { + finishTest(); + } + }).fail(new Function(){ + public void f() { + fail(); + } + }); + } + + public void testGetScriptFail() { + delayTestFinish(5000); + String url = "http://127.0.0.1/nopage"; + Ajax.getScript(url) + .done(new Function(){ + public void f() { + fail(); + } + }).fail(new Function(){ + public void f() { + finishTest(); + } + }); + } + } -- 2.39.5