From: Manolo Carrasco Date: Wed, 14 Jan 2015 22:54:43 +0000 (+0100) Subject: Utility method to export a function X-Git-Tag: gwtquery-project-1.4.3~1 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=2413750e9679d1a1ac38d961c144e1189f8f1080;p=gwtquery.git Utility method to export a function --- 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 e1c0842b..1012a8dc 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 @@ -228,6 +228,20 @@ public class JsUtils { } } + /** + * Assign a function to a property of the window object. + */ + public static void export(String name, Function f) { + export(GQuery.window, name, f); + } + + /** + * Export a function as a property of a javascript object. + */ + public static void export(JavaScriptObject o, String name, Function f) { + prop(o, name, (Object)(f != null ? wrapFunction(f) : null)); + } + /** * Camelize style property names. * for instance: font-name -> fontName @@ -573,7 +587,7 @@ public class JsUtils { } private static native T runJavascriptFunctionImpl(JavaScriptObject o, String meth, JsArrayMixed args) /*-{ - var f = o || window, p = meth.split('.'); + var f = o || $wnd, p = meth.split('.'); for (var i in p) { o = f; f = f[p[i]]; diff --git a/gwtquery-core/src/test/java/com/google/gwt/query/client/GQueryCoreTestGwt.java b/gwtquery-core/src/test/java/com/google/gwt/query/client/GQueryCoreTestGwt.java index 4df6b85c..54181621 100644 --- a/gwtquery-core/src/test/java/com/google/gwt/query/client/GQueryCoreTestGwt.java +++ b/gwtquery-core/src/test/java/com/google/gwt/query/client/GQueryCoreTestGwt.java @@ -20,6 +20,14 @@ import static com.google.gwt.query.client.GQuery.$$; import static com.google.gwt.query.client.GQuery.document; import static com.google.gwt.query.client.GQuery.window; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; +import java.util.Comparator; +import java.util.List; + +import junit.framework.Assert; + import com.google.gwt.dom.client.Document; import com.google.gwt.dom.client.Element; import com.google.gwt.dom.client.InputElement; @@ -41,6 +49,7 @@ import com.google.gwt.query.client.js.JsNodeArray; import com.google.gwt.query.client.js.JsUtils; import com.google.gwt.user.client.DOM; import com.google.gwt.user.client.Event; +import com.google.gwt.user.client.Window; import com.google.gwt.user.client.ui.Button; import com.google.gwt.user.client.ui.HTML; import com.google.gwt.user.client.ui.Label; @@ -48,14 +57,6 @@ import com.google.gwt.user.client.ui.RootPanel; import com.google.gwt.user.client.ui.TextArea; import com.google.gwt.user.client.ui.Widget; -import junit.framework.Assert; - -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Collections; -import java.util.Comparator; -import java.util.List; - /** * Test class for testing gwtquery-core api. */ @@ -1046,6 +1047,18 @@ public class GQueryCoreTestGwt extends GWTTestCase { assertNull(e); } + public void testUtilsJsniExport() { + JsUtils.export("foo", new Function() { + public Object f(Object... args) { + assertEquals("bar", (String)args[0]); + assertEquals(3.5, (Double)args[1]); + return "OK"; + } + }); + assertTrue(JsUtils.hasProperty(window, "foo")); + assertEquals("OK", JsUtils.jsni("foo", "bar", 3.5)); + } + public void testVal_issue98() { $(e).html("" +""