diff options
author | Manolo Carrasco <manolo@apache.org> | 2015-01-28 17:34:46 +0100 |
---|---|---|
committer | Manolo Carrasco <manolo@apache.org> | 2015-01-29 11:25:58 +0100 |
commit | a115f777053b8558dd5d440ebcc7f36a8fbb2fed (patch) | |
tree | 378a9aabafcb09d51e0d212fbac9169a4f62c4e3 | |
parent | 0b4ca15d994c78cc38e35cbe691f02b73f1fb54b (diff) | |
download | gwtquery-a115f777053b8558dd5d440ebcc7f36a8fbb2fed.tar.gz gwtquery-a115f777053b8558dd5d440ebcc7f36a8fbb2fed.zip |
Adding a couple of methods to import html
-rw-r--r-- | gwtquery-core/src/main/java/com/google/gwt/query/client/plugins/ajax/Ajax.java | 49 |
1 files changed, 44 insertions, 5 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 eb58f93d..814a58e2 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 @@ -29,6 +29,8 @@ import com.google.gwt.query.client.Promise; 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.user.client.Timer; import com.google.gwt.user.client.ui.FormPanel; /** @@ -311,6 +313,9 @@ public class Ajax extends GQuery { return get(url, (IsProperties) data, null); } + /** + * @deprecated Use promises instead + */ public static Promise get(String url, IsProperties data, Function onSuccess) { Settings s = createSettings(); s.setUrl(url); @@ -386,11 +391,16 @@ public class Ajax extends GQuery { } public static Promise loadScript(final String url, Function success) { - return ajax(createSettings() - .setUrl(url) - .setType("get") - .setDataType("loadscript") - .setSuccess(success)); + GQuery script = $("script[src^='" + url + "']"); + if (script.isEmpty()) { + return ajax(createSettings() + .setUrl(url) + .setType("get") + .setDataType("loadscript") + .setSuccess(success)); + } else { + return Deferred().resolve().promise(); + } } public static Promise post(String url, IsProperties data) { @@ -452,4 +462,33 @@ public class Ajax extends GQuery { ajax(s); return this; } + + public static Promise loadLink(String rel, String url) { + GQuery link = $("link[rel='" + rel + "'][href^='" + url + "']"); + if (link.isEmpty()) { + return new PromiseFunction() { + public void f(Deferred dfd) { + GQuery link = $("<link rel='" + rel + "' href='" + url + "'/>"); + link.on("load", new Function() { + public void f() { + // load event is fired before the imported stuff has actually + // being ready, we delay it to be sure it is ready. + new Timer() { + public void run() { + dfd.resolve(); + } + }.schedule(100); + } + }); + $(document.getHead()).append(link); + } + }; + } else { + return Deferred().resolve().promise(); + } + } + + public static Promise importHtml(String url) { + return loadLink("import", url); + } } |