diff options
author | Manuel Carrasco Moñino <manuel.carrasco.m@gmail.com> | 2013-11-16 19:58:02 +0100 |
---|---|---|
committer | Manuel Carrasco Moñino <manuel.carrasco.m@gmail.com> | 2013-11-16 19:58:02 +0100 |
commit | 6a23c0c2028f4d97f8dd8b0094368bb69e09b7a1 (patch) | |
tree | 4c15d3ba8f179ca285af4d6acac47817bff5b72d | |
parent | 7425ebf906a878e061cba0a08525ed217f2977c0 (diff) | |
download | gwtquery-6a23c0c2028f4d97f8dd8b0094368bb69e09b7a1.tar.gz gwtquery-6a23c0c2028f4d97f8dd8b0094368bb69e09b7a1.zip |
Fix getScript so as once it is downloaded it is executed like in jQuery. Move loadScript from JsUtils to Ajax
-rw-r--r-- | gwtquery-core/src/main/java/com/google/gwt/query/client/js/JsUtils.java | 4 | ||||
-rw-r--r-- | gwtquery-core/src/main/java/com/google/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<Void, Exception>() { + ScriptInjector.fromUrl(url).setWindow(window) + .setCallback(new Callback<Void, Exception>() { 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); |