Monday, October 10, 2016

Re: jsinterop @JsFunction: How to get this (context)?

Thanks for the confirmation.

Here is how I did it. The following is simplified and minimized jsinterop from D3 bindings.

The function at hand selection.selectAll takes a callback (MultiSelectorA) that has set context (this) of the current matching element.

Here is how I did it for future reference...
@JsType(isNative = true, namespace = JsPackage.GLOBAL, name = "d3")                                                                                                                                                                                                                                                                         public class D3 {                                                                                                                                                                                                                                                                                                                               @JsFunction                                                                                                                                                                                                                                                                                                                                 public interface MultiSelectorA<T> {                                                                                                                                                                                                                                                                                                            public Element[] find(Element context, T d, int i, int nodes);                                                                                                                                                                                                                                                                     }  
/* we need this in order to pass the this argument into our java callback */ private static class Wrapper { public static native JavaScriptObject wrap( MultiSelectorA selector) /*-{                         return function(a, b, c) {                                 return selector(this, a, b, c);                         }         }-*/; } @JsType(isNative = true) public static class Selection { /**          * @todo TODO:          *          * Requirement @JsFunction can access the context (this)          * <ul>          * <li>Enable (uncomment):</li>          * <ul>          * <li>native selectAll(MultiSelectorA selector)</li>          * </ul>          * <li>Delete:</li>          * <ul>          * <li>private native Selection _selectAll(JavaScriptObject callback)</li>          * <li>private static class Wrapper</li>          * <li>public @JsOverlay selectAll(MultiSelectorA selector)</li>          * <li>Make Selection interface from concrete class</li>          * </ul>          *          **/ // public native Selection selectAll(MultiSelectorA selector); @JsMethod(name = "selectAll") private native Selection _selectAll(JavaScriptObject callback); @JsOverlay public final Selection selectAll(MultiSelectorA selector) { return _selectAll(Wrapper.wrap(selector)); } } }


On Mon, Oct 10, 2016 at 5:23 AM, Stepan Koltsov <stepan.koltsov@gmail.com> wrote:
Seems like it is not possible. I had to create jsni stub to grab this and pass it as parameter.

--
You received this message because you are subscribed to the Google Groups "GWT Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-web-toolkit+unsubscribe@googlegroups.com.
To post to this group, send email to google-web-toolkit@googlegroups.com.
Visit this group at https://groups.google.com/group/google-web-toolkit.
For more options, visit https://groups.google.com/d/optout.



--
Vassilis Virvilis

--
You received this message because you are subscribed to the Google Groups "GWT Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-web-toolkit+unsubscribe@googlegroups.com.
To post to this group, send email to google-web-toolkit@googlegroups.com.
Visit this group at https://groups.google.com/group/google-web-toolkit.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment