From 6a23c0c2028f4d97f8dd8b0094368bb69e09b7a1 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Manuel=20Carrasco=20Mo=C3=B1ino?= Date: Sat, 16 Nov 2013 19:58:02 +0100 Subject: [PATCH] Fix getScript so as once it is downloaded it is executed like in jQuery. Move loadScript from JsUtils to Ajax --- .../google/gwt/query/client/js/JsUtils.java | 4 +++ .../gwt/query/client/plugins/ajax/Ajax.java | 26 ++++++++++++++++--- 2 files changed, 27 insertions(+), 3 deletions(-) diff --git a/gwtquery-core/src/main/java/com/google/gwt/query/client/js/JsUtils.java b/gwtquery-core/src/main/java/com/google/gwt/query/client/js/JsUtils.java index 89b1ab57..9790b12f 100644 --- a/gwtquery-core/src/main/java/com/google/gwt/query/client/js/JsUtils.java +++ b/gwtquery-core/src/main/java/com/google/gwt/query/client/js/JsUtils.java @@ -26,6 +26,7 @@ import com.google.gwt.dom.client.NodeList; import com.google.gwt.query.client.Function; import com.google.gwt.query.client.GQuery; import com.google.gwt.query.client.Properties; +import com.google.gwt.query.client.plugins.ajax.Ajax; import com.google.gwt.user.client.Command; import com.google.gwt.user.client.DOM; @@ -371,7 +372,10 @@ public class JsUtils { /** * Load an external javascript library. The inserted script replaces the * element with the given id in the document. + * + * @deprecated use {@link com.google.gwt.query.client.plugins.ajax.Ajax#loadScript(String)} */ + @Deprecated public static void loadScript(String url, String id) { GQuery gs = GQuery.$(DOM.createElement("script")); GQuery gp = GQuery.$("#" + id).parent(); 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 d50d7470..3c834330 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 @@ -128,7 +128,7 @@ public class Ajax extends GQuery { if ("jsonp".equalsIgnoreCase(dataType)) { ret = new PromiseReqBuilderJSONP(url, null, settings.getTimeout()); - } else if ("script".equalsIgnoreCase(dataType)){ + } else if ("loadscript".equalsIgnoreCase(dataType)){ ret = createPromiseScriptInjector(url); } else { ret = createPromiseRequestBuilder(settings, httpMethod, url, data) @@ -144,6 +144,9 @@ public class Ajax extends GQuery { retData = JsUtils.parseJSON(response.getText()); } else { retData = response.getText(); + if ("script".equalsIgnoreCase(dataType)) { + ScriptInjector.fromString((String)retData).setWindow(window).inject(); + } } } catch (Exception e) { if (GWT.getUncaughtExceptionHandler() != null) { @@ -212,7 +215,8 @@ public class Ajax extends GQuery { private static Promise createPromiseScriptInjector(final String url) { return new PromiseFunction() { public void f(final Deferred dfd) { - ScriptInjector.fromUrl(url).setCallback(new Callback() { + ScriptInjector.fromUrl(url).setWindow(window) + .setCallback(new Callback() { public void onSuccess(Void result) { dfd.resolve(); } @@ -349,7 +353,7 @@ public class Ajax extends GQuery { } /** - * Load a JavaScript file from the server using a GET HTTP request, then execute it. + * Get a JavaScript file from the server using a GET HTTP request, then execute it. */ public static Promise getScript(String url) { return getScript(url, null); @@ -364,6 +368,22 @@ public class Ajax extends GQuery { ); } + /** + * Load a JavaScript file from any url using the script tag mechanism + */ + public static Promise loadScript(String url) { + return loadScript(url, null); + } + + public static Promise loadScript(final String url, Function success) { + return ajax(createSettings() + .setUrl(url) + .setType("get") + .setDataType("loadscript") + .setSuccess(success) + ); + } + public static Promise post(String url, Properties data, final Function onSuccess) { Settings s = createSettings(); s.setUrl(url); -- 2.39.5