From: Manolo Carrasco Date: Wed, 28 Jan 2015 16:34:46 +0000 (+0100) Subject: Adding a couple of methods to import html X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=a115f777053b8558dd5d440ebcc7f36a8fbb2fed;p=gwtquery.git Adding a couple of methods to import html --- 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.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); + } }